How to convert a list of integers to a string in Python? You can use list comprehension to convert a list of integers into a list strings in one line very efficiently. You can use other ways of Python to convert a list of integers to a string like map()
, enumerate()
, for loop, format()
, sorted()
, functools.reduce()
, and recursive functions. In this article, I will explain how to convert a list of integers to a string by using all these methods with examples.
Related: You can also remove numbers from Python string.
1. Quick Examples of Converting List of Integers to String
If you are in a hurry, below are some quick examples of converting a list of integers to a string.
# Quick examples of converting list of integers to string
from functools import reduce
# Initialize a list of integers
list_int = [2, 5, 12, 8, 16]
# Example 1: Using list comprehension
# Convert a list of integers to a string
result = [str(x) for x in list_int]
# Example 2: Use map() method
# Convert a list of integers to a string
list_string = map(str, list_int)
result = list(list_string)
# Example 3: Using enumerate() function
# Convert a list of integers to a string
result=[]
for i,a in enumerate(list_int):
result.append(str(a))
# Example 4: Using iteration
# Convert a list of integers to a string
result = []
for i in list_int:
result.append(str(i))
# Example 5: Using format() function
# Convert a list of integers to a string
result = [format(x, 'd') for x in list_int]
# Example 6: Using sorted() function with key parameter
# Convert a list of integers to a string
result = sorted(map(str, list_int), key=int)
# Example 7: Using functools.reduce() method
# Convert a list of integers to a string
result = reduce(lambda a, b: a+[str(b)], list_int, [])
# Example 8: Using a recursive method
# Convert a list of integers to a string
def convert_to_int(lst):
if len(lst) == 0:
return []
else:
return [str(lst[0])] + convert_to_int(lst[1:])
list_int_sorted = sorted(list_int)
result = convert_to_int(list_int_sorted)
2. Using List Comprehension
To convert a list of integers to a list of strings use list comprehension. For example, first, Initialize the list of integers list_int
. Then use a list comprehension to iterate through each element x
in list_int
. Within the list comprehension, it converts each integer element x
to its string representation using str(x)
. The result is a new list called result
, which contains the string representations of the integers.
# Initialize list of integers
list_int = [2, 5, 12, 8, 16]
print("List of integers:",list_int)
# Using list comprehension
# Convert list of integers to string
result = [str(x) for x in list_int]
print("List of strings:", result)
Yields below output.
So, the list_int
has been successfully converted into a list of strings in the result variable.
3. Convert List of Integers to String Using map() Method
You can also use the map() function to convert a list of integers to a list of strings. For instance, the first list_int
is initialized as a list containing integers [2, 5, 12, 8, 16]
. Then apply map() function over the to each element in list_int
and convert each integer to its corresponding string representation using str()
function. The result of the map()
operation is iterable, so it’s converted to a list using list(). Finally, the converted list of strings is stored in the result
variable.
# Initialize list of integers
list_int = [2, 5, 12, 8, 16]
print("List of integers:",list_int)
# Use map() method
# Convert list of integers to string
list_string = map(str, list_int)
result = list(list_string)
print("List of strings:", result)
Yields the same output as above.
4. Using enumerate() Function
To convert a list of integers to strings using the enumerate() function in a for loop. For instance, first, Initialize the list of integers list_int
. Then use a for loop along with enumerate()
to iterate through a list list_int
. In each iteration, it retrieves both the index i
and the integer value a
. Converts the integer value a
to its string representation using str(a)
and appends it to the result
list. Finally, it prints the result
list, which contains the string representations of the integers.
# Initialize list of integers
list_int = [2, 5, 12, 8, 16]
print("List of integers:",list_int)
# Using enumerate() function
# Convert list of integers to string
result=[]
for i,a in enumerate(list_int):
result.append(str(a))
print("List of strings:", result)
Yields the same output as above.
5. Convert List of Integers to String Using For Loop
To convert the list of integers to a list of strings using for loop. For instance, firs,t Initialize the list of integers list_int
. Then use a for
loop to iterate through each element i
in list_int
. Within the loop, it converts each integer element i
to its string representation using str(i)
. The result is a new list called result
, which contains the string representations of the integers.
# Initialize list of integers
list_int = [2, 5, 12, 8, 16]
print("List of integers:",list_int)
# Using iteration
# Convert list of integers to string
result = []
for i in list_int:
result.append(str(i))
print("List of strings:", result)
Yields the same output as above.
6. Convert List of Integers to String Using format() Function
You can also use another valid way to convert a list of integers to a list of strings, using the format()
function with the 'd'
format specifier.
In the below example, first, initialize the list of integers list_int
. Then use a list comprehension to iterate through each element x
in list_int
. Within the list comprehension, it converts each integer element x
to its string representation using format(x, 'd')
, the where 'd'
is the format specifier for decimal (base 10) integers. The result is a new list called result
, which contains the string representations of the integers.
# Initialize list of integers
list_int = [2, 5, 12, 8, 16]
print("List of integers:",list_int)
# Using format() function
# Convert list of integers to string
result = [format(x, 'd') for x in list_int]
print("List of strings:", result)
Yields the same output as above.
7. Using sorted() Function with key Parameter
To convert a list of integers to a list of strings use sorted() function. For instance, initialize the list of integers list_int
. Then apply the map()
function over the given list to convert each element in list_int
to its string representation.
And then apply the sorted() function to the resulting list of strings and specify sorting should be based on the integer value of each string, not their lexicographic (string) order setting its key
parameter as key=int
. The result is a new list called result
, which contains the string representations of the integers sorted in ascending order based on their integer values.
# Initialize list of integers
list_int = [2, 5, 12, 8, 16]
print("List of integers:",list_int)
# Using sorted() function with key parameter
# Convert list of integers to string
result = sorted(map(str, list_int), key=int)
print("List of strings:", result)
# Output:
# List of integers: [2, 5, 12, 8, 16]
# After converting a list of integers to a string: ['2', '5', '8', '12', '16']
8. Using functools.reduce() Method
To convert a list of integers to a list of strings using the functools.reduce()
method along with a lambda function. For example, initialize the list of integers list_int
and import the reduce
function from the functools
module. The use the reduce() method to apply a lambda function to each element b
in list_int
. The lambda function converts each integer b
to its string representation and appends it to the accumulator a
. The initial accumulator value is an empty list []
.
The reduce()
method iterates through list_int
, applying the lambda function to each element and accumulating the results. The result is a new list called result
, which contains the string representations of the integers.
from functools import reduce
# Initialize list of integers
list_int = [2, 5, 12, 8, 16]
print("List of integers:",list_int)
# Using functools.reduce() method
# Convert list of integers to string
result = reduce(lambda a, b: a+[str(b)], list_int, [])
print("List of strings:", result)
Yields the same output as above.
9. Using Recursive Method
You can also use recursive approach to convert a list of integers to a list of strings. For example, first, initialize the list of integers list_int
and define a recursive function convert_to_int(lst)
to convert a list of integers to a list of strings. The base case checks if the input list lst
is empty. If it’s empty, it returns an empty list ([]
). In the recursive case, it converts the first element of the list to a string using str(lst[0])
and then concatenates it with the result of the recursive call on the rest of the list (lst[1:]
).
Creates a sorted version of list_int
using sorted(list_int)
. This step is not necessary for the conversion itself but seems to be included for sorting purposes. Calls the convert_to_int()
function with the sorted list as an argument, storing the result in the result
variable. Finally, it prints the result
list, which contains the string representations of the integers.
# Initialize list of integers
list_int = [2, 5, 12, 8, 16]
print("List of integers:",list_int)
# Using recursive method
# Convert list of integers to string
def convert_to_int(lst):
if len(lst) == 0:
return []
else:
return [str(lst[0])] + convert_to_int(lst[1:])
list_int_sorted = sorted(list_int)
result = convert_to_int(list_int_sorted)
print("List of strings:", result)
Yields the same output as above.
Conclusion
In this article, I have explained how to convert a list of integers to a string in Python by using list comprehension, map()
, enumerate()
, iteration, format()
, sorted()
, functools.reduce()
, and recursive functions with examples.
Happy Learning !!
Related Articles
- Python String Sort
- Python String center() Method
- Python String count() Method
- Python String Unicode
- Python String Remove Numbers
- Python String Startswith
- Python String to Boolean
- Python String endswith
- Python String Copy
- Python String Concatenation
- Python String Formatting Explained
- Python String Methods
- Concatenate String and Int in Python
- Difference Between List and Dictionary in Python
- Find Maximum Value in List in Python
- Find Difference of Lists in Python
- How to Append Dictionary to List in Python?
- How to check Python Version?