If you’re working with data in Python, you may sometimes need to convert strings to integers. This can be especially tricky when the strings are contained in a list. So today, we’ll show you how to use map() to convert strings to integers in a Python list. Keep reading to learn more!
What is the
map() in Python?
map() function is a Python built-in function that allows you “map” or applies a function to an iterable object, such as a list. The
map() function takes two arguments: the first is the name of the function you want to apply, and the second is an iterable object that needs to be applied.
How to use
map() to convert strings to integers in a Python list?
In this case, we want to convert all of the strings in our list to integers using the
map() function. To do this, we must create a new function that takes a string as an argument and returns an integer. We can call this function whatever we want, but for the sake of simplicity, we’ll just call it “
An example of using
map() to convert strings to integers in a list
We can now use the
map() function with the
convertToInt() function to convert all of the strings in our list into integers. Assume we have the following string list:
['0', '2', '4', '1', '3', '5']
We can use the
map() function as follows:
numberStrings = ['0', '2', '4', '1', '3', '5'] # Declare a function to convert a string to an int def convertToInt(string): return int(string) # Convert strings to integers in a list using map() mapObj = map(convertToInt, numberStrings) # Convert the map object to a list result = list(mapObj) print(result) # [0, 2, 4, 1, 3, 5]
[0, 2, 4, 1, 3, 5]
map() returns a map object, remember to use the
list() function to convert it back to a list!
Another way to convert strings to integers in a list
map(), there is a faster approach to convert the strings in a list to integers. That is using list comprehension.
List comprehensions allow you to concisely apply a function to every element in a list, creating a new list as a result.
map(), we will not have to create the
convertToInt() function because we will use int() directly in the list comprehension. As an example:
numberStrings = ['0', '2', '4', '1', '3', '5'] # Use the list comprehension to convert strings to integers in a list quickly result = [int(v) for v in numberStrings] print(result) # [0, 2, 4, 1, 3, 5]
[0, 2, 4, 1, 3, 5]
As you can see, list comprehension requires less code while getting the same output as
map() function is a simple way to convert strings to numbers in a list. Since
map() applies a function to each element of a list, all you have to do is creating a function that converts a string to an integer and passes it into the
map(). Try list comprehension if you need something faster and more concise (but more complex to comprehend than the
map()). We hope you like this article about how to use map() to convert strings to integers in a Python list.
Have a great day!
Maybe you are interested:
- Convert a list of integers to a list of floats in Python
- Convert a Map object to a List in Python
- Convert a list of characters into a string in Python
Name of the university: HCMUE