Python Map to Int

Rate this post

Problem Formulation

Convert a string list to an integer list using the map() function.


Input:     ['1', '2', '3']
Output:    [1, 2, 3]

Input:     ['42']
Output:    [42]

Input:     ['-1', '-2', '-3']
Output:    [-1, -2, -3]

Solution Using Map

To convert a list of strings to a list of integers, you can “map to int” by calling map(int, lst) on the int built-in function object as first and the lst object as second argument. The result is a map object that you can convert back to a list using the list() function.

For example, the one-liner list(map(int, ['1', '2', '3'])) converts the string list ['1', '2', '3'] to the integer list [1, 2, 3].

lst = ['1', '2', '3']
res = map(int, lst)

The value stored in the variable res is a map object that is not readable. If you print it as is, it’ll give us an unreadable output like this:

<map object at 0x0000016175ACDDF0>

To fix this, you can convert the map object back to a list by using the list() built-in function.

The output after conversion looks prettier: 🤩

# [1, 2, 3]

In some cases, however, you don’t need to convert the map object to a list. For example, the map object also allows iteration in a for loop so you don’t need to convert it to a list:

lst = ['1', '2', '3']
res = map(int, lst)

for x in res:  # <-- NOT a list but a map object

The output is, of course:


Understanding the Map Function

Feel free to check out my in-depth tutorial on the map() function or just watch this quick video tutorial:

Mastering the Python Map Function [+Video]

The map() function transforms one or more iterables into a new one by applying a “transformator function” to the i-th elements of each iterable.

The arguments are the transformator function object and one or more iterables.

🚀 Expert Tip: If you pass n iterables as arguments, the transformator function must be an n-ary function taking n input arguments.

The return value is an iterable map object of transformed, and possibly aggregated elements.

The map object is an iterator that saves all mapped elements so that you can iterate over them. Especially for large iterables this is more efficient than a standard Python list.

Better Solution Using List Comprehension

I wrote a whole tutorial on converting a string list to an integer list—Python is flexible enough to provide us with multiple ways to accomplish this:

The take-away of my blog article is the following:

The most Pythonic way to convert a list of strings to a list of integers is to use the list comprehension [int(x) for x in strings]. It iterates over all elements in the list and converts each list element x to an integer value using the int(x) built-in function.

Here’s our example using this approach:

lst = ['1', '2', '3']
res = [int(x) for x in lst]
# [1, 2, 3]

Why is list comprehension better than map() in my opinion?

The advantages compared to map() are slight performance benefits and improved readability because we don’t “stack” function calls like this: list(map(...)).

Understanding List Comprehension

How does list comprehension generally work?

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.

The example [x for x in range(3)] creates the list [0, 1, 2]:

>>> [x for x in range(3)]
[0, 1, 2]

The example [int(x) for x in ['1', '2', '3']] converts each string element in ['1', '2', '3'] to an integer — by using the EXPRESSION int(x) — and creates the integer list [1, 2, 3]:

>>> [int(x) for x in ['1', '2', '3']]
[1, 2, 3]

If you need to catch up with list comprehension, feel free to watch the following video tutorial:

A Simple Introduction to List Comprehension in Python

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!