Convert Map Object to List in Python (3 Examples)

 

Hi! This tutorial will demonstrate how to transform a map object into a list in the Python programming language.

But first, here is a quick overview of the tutorial:

Let’s dive into Python code!

 

Create Sample Tuple & List Objects

We will create a sample tuple of integers and list of integers, and another list of character string values that we will use in this tutorial. So, in your preferred Python IDE, run the lines of code below.

tuple_obj = (5,10,15,20)
my_list = [5,10,15,20]
names = ["Andy", "Hillary", "Josh", "Lisa"]

 

Example 1: Create Map Object from Tuple & Transform to List

In this first example, we will create a custom function that will square any value parsed to it and then iteratively apply that function to the numeric values in our tuple to create a map object using the map() function, which can iterate over both tuples and lists.

def multiply(a):
  return a ** 2
 
map_obj = map(multiply, tuple_obj)

We can confirm that it is indeed a map object by running the type() function.

print(type(map_obj))
 
# <class 'map'>

Next, we will turn the map object into a list by running the list() function.

list_obj = list(map_obj)
print(list_obj)
 
# [25, 100, 225, 400]
 
 
print(type(list_obj))
 
# <class 'list'>

As seen above, the list() function converted the map object into a list.

 

Example 2: Create Map Object from List of Integers & Transform to List

In this second example, we will create a map object with a lambda function that will double any value parsed to it. We will apply that function to “my_list”.

map_obj = map(lambda x: x + x, my_list)
print(type(map_obj))
 
# <class 'map'>

Then convert it to a list called list_obj.

list_obj = list(map_obj)
print(list_obj)
 
# [10, 20, 30, 40]
 
 
print(type(list_obj))
 
# <class 'list'>

See, just like in Example 1; the map object was transformed into a list.

 

Example 3: Create Map Object from List of Character Strings & Transform to List

In this third and final example, we will create a map object from the “names” list that we created formerly using the map() function to make the strings uppercase and then convert that map object into a list.

map_obj = map(str.upper, names)
print(type(map_obj))
 
# <class 'map'>

Let’s now transform it into a list!

list_obj = list(map_obj)
print(list_obj)
# ['ANDY', 'HILARY', 'JOSH', 'LISA']
 
print(type(list_obj))
# <class 'list'>

As we have seen in the 3 examples above, it is very easy to create a map object in Python and convert that object into a list using the list() function.

 

Video, Further Resources & Summary

Do you need more explanations on how to convert a map object to a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to convert a map object to a list in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these:

This post has shown how to convert a map object to a list in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top