How to convert a Map object to a List in Python
The map()
function in Python returns a map object (an iterator) that applies a function to every item in an iterable.
This guide explores various methods for converting map objects to lists, allowing you to work with the results in a standard list format. We will cover using the list()
constructor, iterable unpacking, list comprehensions, and loops.
Converting Map Objects with the list()
Constructor
The simplest way to convert a map object to a list is by using the list()
constructor. The list()
constructor takes an iterable (like a map object) and returns a new list object containing all the items from the iterable.
my_list = ['1.1', '2.2', '3.3']
new_list = list(map(float, my_list))
print(new_list) # Output: [1.1, 2.2, 3.3]
print(type(new_list)) # Output: <class 'list'>
- The
map(float, my_list)
creates amap
object which applies the functionfloat()
to each item in themy_list
. - The
list()
constructor then converts this map object into a new list.
Iterating over Map Objects
If you only need to access the elements from the map object, you can iterate directly using a for loop:
my_list = ['1.1', '2.2', '3.3']
map_obj = map(float, my_list)
for item in map_obj:
print(item)
output
1.1
2.2
3.3
Converting Map Objects with Iterable Unpacking
Another way to convert a map object to a list is by using the iterable unpacking operator (*
). This operator unpacks the map object's elements, allowing them to be collected into a list:
my_list = ['1.1', '2.2', '3.3']
new_list = [*map(float, my_list)]
print(new_list) # Output: [1.1, 2.2, 3.3]
print(type(new_list)) # Output: <class 'list'>
- The
*
operator unpacks the elements from themap
object which causes the list to be created.
Using List Comprehensions Instead of map()
List comprehensions offer a concise way to achieve the same result as using map()
, allowing you to apply an operation and collect the result into a list:
my_list = ['1.1', '2.2', '3.3']
new_list = [float(item) for item in my_list]
print(new_list) # Output: [1.1, 2.2, 3.3]
- The list comprehension
[float(item) for item in my_list]
iterates over eachitem
in themy_list
, converts it to afloat
and stores it in the new list.
Converting Map Objects with a for
Loop
You can also convert a map object to a list by iterating over the map object using a for
loop and appending each value to the new list:
my_list = ['1.1', '2.2', '3.3']
map_obj = map(float, my_list)
new_list = []
for item in map_obj:
new_list.append(item)
print(new_list) # Output: [1.1, 2.2, 3.3]
This approach allows more flexibility within the for loop, and performs the exact same function as the list()
constructor approach.
Using a for
Loop Instead of map()
You can achieve the same result using a for loop directly on the original list:
my_list = ['1.1', '2.2', '3.3']
new_list = []
for item in my_list:
new_list.append(float(item))
print(new_list) # Output: [1.1, 2.2, 3.3]
This creates an empty list new_list
, then iterates over the original list and converts each item to float
and appends the result to new_list
.