Warning: session_start(): open(/tmp/sess_98687839774a1ed6ae7a8fd543a34dd4, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
Convert A Map Object To A List In Python - LearnShareIT

Convert A Map Object To A List In Python

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.

Summary

If you have any questions about how to convert a map object to a list in Python, leave a comment below. I will answer your questions. Thank you for reading!

Maybe you are interested:

Leave a Reply

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