Convert a Map object to a List in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# Convert a Map object to a List in Python

Use the list() class to convert a map object to a list.

The list class takes an iterable (such as a map object) as an argument and returns a list object.

main.py
my_list = ['1.1', '2.2', '3.3'] new_list = list(map(float, my_list)) print(new_list) # ๐Ÿ‘‰๏ธ [1.1, 2.2, 3.3] print(type(new_list)) # ๐Ÿ‘‰๏ธ <class 'list'>

convert map object to list

The code for this article is available on GitHub

We passed a map object to the list() class to convert it to a list.

The list() class takes an iterable and returns a list object.

The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

The map() function in the example converts each item from the original list to a float.

# Iterating over a Map object

If you only need to iterate over the map object, use a basic for loop.

main.py
my_list = ['1.1', '2.2', '3.3'] map_obj = map(float, my_list) for item in map_obj: # 1.1 # 2.2 # 3.3 print(item)

iterating over map object

The code for this article is available on GitHub

The item variable gets set to the current map item on each iteration.

# Convert a Map object to a List using iterable unpacking

You can also use the * iterable unpacking operator to convert a map object to a list.

main.py
my_list = ['1.1', '2.2', '3.3'] new_list = [*map(float, my_list)] print(new_list) # ๐Ÿ‘‰๏ธ [1.1, 2.2, 3.3] print(type(new_list)) # ๐Ÿ‘‰๏ธ <class 'list'>

convert map object to list using iterable unpacking

The code for this article is available on GitHub

The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.

main.py
result = [*(1, 2), *(3, 4), *(5, 6)] print(result) # ๐Ÿ‘‰๏ธ [1, 2, 3, 4, 5, 6]
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.

# Using a list comprehension instead of the map() function

An alternative approach would be to directly use a list comprehension.

main.py
my_list = ['1.1', '2.2', '3.3'] new_list = [float(i) for i in my_list] print(new_list) # ๐Ÿ‘‰๏ธ [1.1, 2.2, 3.3]

using list comprehension instead of map function

The code for this article is available on GitHub
List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

In the example, we explicitly pass each list item to the float() class instead of doing it implicitly as we did with the map() function.

# Convert a Map object to a List using a for loop

You can also use a for loop to convert a map object to a list.

main.py
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) # ๐Ÿ‘‰๏ธ [1.1, 2.2, 3.3]

convert map object to list using for loop

We declared a new variable and initialized it to an empty list and used a for loop to iterate over the map object.

On each iteration, we append the current item to the new list.

# Using a for loop instead of the map function

You can also use a for loop as a replacement for the map function.

main.py
my_list = ['1.1', '2.2', '3.3'] new_list = [] for item in my_list: new_list.append(float(item)) print(new_list) # ๐Ÿ‘‰๏ธ [1.1, 2.2, 3.3]

using for loop instead of map function

The code for this article is available on GitHub

We used a for loop to iterate over the list.

On each iteration, we convert the current item to a float and append the result to a new list.

As shown in the previous examples, the same can be achieved using a list comprehension or the map() function.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev