# Convert A Map Object To A List In Python

To convert a map object to a list in Python, you can use the list() method or use the iterable unpacking operator “*“. Follow the article to understand better.

## What is the map() function in Python?

In Python, the built-in map() function iterates the elements of an iterable( list, tuple, dict, set,…) through a given function (e.g. chr(),… ) and returns a list of results when it’s done. You use this function when you want to use a single function to convert for all iterable elements. Iterable and function are passed as arguments to map() in Python.

Syntax:

map(function, iterable, …)

The map() function returns values ​​called map object.

Example:

numTuple = (1, 2, 3, 4, 5)
resultMap = map(int, numTuple)
print(type(resultMap)) # <class 'map'>

The problem here is I want to convert a map object to a list.

## Convert a map object to a list in Python

### Use the list() method

In Python, ‘ list’ is a data type to archive many other types of data and is allowed access to the elements in the ‘list’. A ‘list’ can include data types such as integers, strings, and objects. ‘List’ is mutable even when created. The elements in the ‘list’ are ordered and have a certain number. The elements in ‘list’ are indexed in a sequence, and by default, the first index of the list is 0.

Syntax:

list([iterable])

Parameters:

• iterable (optional): the object can be a string, tuple, set, dictionary, or iterator.

The list() function returns:

• list() creates an empty list if no parameters are passed.
• list() creates a list of elements in the iterable if the iterable is passed as a parameter.

Example:

numTuple = (1, 2, 3, 4, 5)
resultMap = map(int, numTuple)

# Using list() to convert map object to a list
resultList = list(resultMap)
print(f"resultList: {resultList}")
print(f"Type of resultList: {type(resultList)}")

Output:

resultList: [1, 2, 3, 4, 5]
Type of resultList: <class 'list'>

The example above map() function returns the squared values of the iterable ‘numTuple‘. The values [1, 2, 3, 4, 5] are called map objects, and then I take the map object passed in ‘list’ and output ‘list’ to the screen.

### Use the iterable unpacking operator “*”

The * is used as the iterable unpacking operator. The advantage of using the * operator is that you can unpack lists and other containers like tuple, string, generator, dict, and set.

Example:

numTuple = (1, 2, 3, 4, 5)

# Using the * operator to convert map object to list
resultList = [*map(int, numTuple)]
print(f"resultList: {resultList}")
print(f"Type of resultList: {type(resultList)}")

Output:

resultList: [1, 2, 3, 4, 5]
Type of resultList: <class 'list'>

In the above example, I use the unpacking operator ‘*’ to unpack an iterable in the map() function.