Convert Map object to list in Python
Python provides a map()
function that you can use to apply a specific function to all given elements in any specified iterable object. This function returns an iterator itself as output. Mapping objects can also be converted into sequence objects such as tuples and lists using their own factory functions.
This tutorial will discuss and demonstrate different methods that can be used to convert a Map object to a list in Python.
list()
Use the method to convert a Map object into a list
in Python
Lists are a part of the four built-in data types provided in Python and can be used to store multiple items in a single variable. Lists are ordered, mutable, and have a definite number of items.
list()
The function is used to create a list object in Python. This method is used to convert a specific tuple into a list. The following code uses list()
the method to convert a Map object into a list in Python:
a = list(map(chr, [70, 50, 10, 96]))
print(a)
Output:
['F', '2', '\n', '`']
In Python 3, many operations or processes performed on iterators return the iterators themselves; this further simplifies the language. This also leads to better and more efficient programs.
Convert Map object to list using list comprehension in Python
List comprehension method is a relatively short and very elegant way to create a list from the given values of an existing list. This method can be used in this case and create a list from a Map object by simple iteration.
The following program uses this method to convert a Map object to a list in Python:
a = [chr(i) for i in [70, 50, 10, 96]]
print(a)
Output:
['F', '2', '\n', '`']
*
Converting Map Objects to Lists
Using Iterable Unpacking Operators in Python
In Python, the term 解包
can be defined as an operation whose main purpose is to assign an iterable object with all the values to a list or tuple, provided it is done in a single assignment statement.
The star *
symbol is used as iterable unpacking operator. Iterable unpacking operator works well and efficiently on tuples and lists.
The following code uses the iterable unpacking operator *
to convert a Map object to a list in Python:
a = [*map(chr, [70, 50, 10, 96])]
print(a)
Output:
['F', '2', '\n', '`']
In most cases, this method is more efficient than the other two. Still, the margins as the iterable unpacking operator is very small, and *
the method is shorter than one character in a list comprehension. All three methods work well, and you can use any of these methods in regular, everyday programming.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Finding a string in a list in Python
Publish Date:2025/05/09 Views:75 Category:Python
-
This tutorial shows you how to find elements from a Python list that have a specific substring in them. We will use the following list and extract ack the strings that have in it. my_list = [ "Jack" , "Mack" , "Jay" , "Mark" ] for Find elem
Getting list shape in Python
Publish Date:2025/05/09 Views:139 Category:Python
-
In Python, knowing the shape of a list is very important for working with data structures, especially when it comes to multidimensional or nested lists. This article explores various ways to determine the shape of a list in Python, from sim
Adding multiple elements to a list in Python
Publish Date:2025/05/09 Views:180 Category:Python
-
List is a mutable data structure in Python. It can contain values of different types. This article will discuss some methods to append single or multiple elements to a Python list. append() Append a single element in a Python list usi
Get the index of the maximum and minimum values in a list in Python
Publish Date:2025/05/09 Views:117 Category:Python
-
In this tutorial, we will discuss methods to get the index of the maximum and minimum value of a list in Python. max() Get the index of the maximum value in a list using and list.index() functions in Python max() The function gives the maxi
List of numbers from 1 to N in Python
Publish Date:2025/05/09 Views:116 Category:Python
-
This tutorial will discuss how to create a list of numbers from 1 to some specified number. Create a user-defined function to create a list of numbers from 1 to N This method will take the desired number from the user and for iterate until
Convert List to Pandas DataFrame in Python
Publish Date:2025/05/09 Views:133 Category:Python
-
This article will show you how to convert items in a list into a Pandas DataFrame. Convert List to Pandas DataFrame in Python DataFrame, in general, is a two-dimensional labeled data structure. Pandas is an open source Python package that i
Sorting a list by another list in Python
Publish Date:2025/05/09 Views:148 Category:Python
-
Normally, when we sort a list, we do it in ascending or descending order. However, we can sort a list based on the order of another list in Python. We will learn how to sort a given list based on the values in another list in this art
Normalizing a list of numbers in Python
Publish Date:2025/05/09 Views:134 Category:Python
-
Normalization means converting the given data to another scale. We rescale the data so that it is between two values. Most of the time, the data is rescaled between 0 and 1. We rescale data for different purposes. For example, machine learn
How to create a list of a specific size in Python
Publish Date:2025/05/09 Views:195 Category:Python
-
Preallocating storage for a list or array is a common practice among programmers when they know the number of elements in advance. Unlike C++ and Java, in Python you must initialize all preallocated storage with some value. Typically, devel