How to Convert an Integer List to a String List in Python

4.8/5 - (6 votes)

The most Pythonic way to convert a list of integers ints to a list of strings is to use the one-liner strings = [str(x) for x in ints]. It iterates over all elements in the list ints using list comprehension and converts each list element x to a string using the str(x) constructor.

This article shows you the simplest ways to convert a one-dimensional list consisting only of integers to a list of strings.

Problem: Given a list of integers [1, 2, 3]. How to convert it to a list of strings ["1", "2", "3"]?

Method 1: List Comprehension

You can use the list comprehension expression [str(x) for x in a] to convert each element of an integer list a to a string list.

You’ll learn how this works in a step-by-step manner next!

Suppose you have a list:

a = [4, 3, 2, 1, 10, 14, -14]

Now, check the type of the list elements:

# <class 'int'>

Let’s apply the built-in function str(), and get a list of strings using list comprehension:

print([str(x) for x in a])
# ['4', '3', '2', '1', '10', '14', '-14']

Okay, now you may ask: How does list comprehension work anyway?

Great question! Here’s the quick and dirty answer:

List comprehension is a compact way of creating lists. The simple formula is [expression + context]. Expression: What to do with each list element? Context: What elements to select? The context consists of an arbitrary number of for and if statements.

You can watch me explain list comprehensions in this video:

A Simple Introduction to List Comprehension in Python

Check the type of numbers in the new list:

A = [str(x) for x in a]
# <class 'str'>

The built-in function str converts an integer to a string representation of that integer.

Thus, it helps us create a new list of strings from the list of integers in a single line of code.

In the previous paragraphs, we’ve touched on a couple of basic Python topics. If you need some more background on those, feel free to check them out on the Finxter blog. Here are the links:

Related Tutorials:

Method 2: Map Function

The built-in function map is well optimized and efficient, when it is called, the elements of the list are retrieved upon access.

Therefore, one element is stored and processed in memory, which allows the program not to store the entire list of elements in the system memory.

You can convert an integer list to a string list by using the expression list(map(str, a)) combining the list() with the map() function. The latter converts each integer element to a string. The former converts the resulting iterable to a list.

Here’s how you apply the map() function to the same list a the following code:

a = [4, 3, 2, 1, 10, 14, -14]
print(list(map(str, a)))
# ['4', '3', '2', '1', '10', '14', '-14']

The map() function returns an iterable map object that we need to convert to a list using the built-in list() constructor.

Method 3: For Loop

Of course, you can also convert a list of ints to a list of strings using a simple for loop.

This is what most people coming from a programming language such as Java and C++ would do as they don’t know the most Pythonic way of using list comprehension, yet (see Method 1).

a = [4, 3, 2, 1, 10, 14, -14]
strings = []

for element in a:

# ['4', '3', '2', '1', '10', '14', '-14']

This basic method to convert a list of ints to a list of strings uses three steps:

  • Create an empty list with strings = [].
  • Iterate over each integer element using a for loop such as for element in list.
  • Convert the int to a string using str(element) and append it to the new string list using the list.append() method.

Method 4: String Formatting for Custom String Conversions

If this is not enough for you, for instance, you need a specific format of the converted strings such as only two digits after the decimal point, you should have a look at Python’s powerful string formatting capabilities.

For example, to convert a list of ints to a list of strings with only two digits, use the string.format() method:

a = [4, 3, 2, 1, 10, 14, -14]
strings = ['{:.2f}'.format(x) for x in a]
# ['4.00', '3.00', '2.00', '1.00', '10.00', '14.00', '-14.00']

Python’s built-in format(value, spec) function transforms input of one format into output of another format defined by you.

Specifically, it applies the format specifier spec to the argument value and returns a formatted representation of value.

For example, format(42, 'f') returns the string representation '42.000000'.

You can watch me introduce the formatting capabilities in this short guide:

Python format() Function: No-BS Guide by Example

To boost your Python skills the easy way, feel free to join my free email academy with lots of free content and cheat sheets—if you haven’t already! πŸ™‚

If you want to go all-in and learn Python while getting paid in the process, check out my Python freelancer course—the number one freelance developer education in the world!

Programming Humor – Python

“I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I’m leaving you.”xkcd