Home > Backend Development > Python Tutorial > How to use the list() function to create a list in Python

How to use the list() function to create a list in Python

王林
Release: 2023-08-22 10:52:46
Original
3403 people have browsed it

How to use the list() function to create a list in Python

How to use the list() function to create a list in Python

Python is a powerful programming language, and one of its very practical functions is lists. A list is a data structure that can store multiple values, which can be of different types. In Python, we can use the list() function to create a list.

The list() function is a built-in function that can convert other iterable objects into a list. Iterable objects can be strings, tuples, sets, dictionaries, etc. Let's look at a few examples to understand how to create a list using the list() function.

First, we can convert a string into a list containing each character. For example, we have a string "Hello World" and we want to convert it into a list:

string = "Hello World"
list_string = list(string)
print(list_string)
Copy after login

The output is: ['H', 'e', ​​'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']

We can see that each character in the string is converted into a elements in the list.

Next, we can convert a tuple into a list. Tuples are immutable sequences, similar to lists, but their elements cannot be changed. For example, we have a tuple (1, 2, 3, 4, 5), we can use the list() function to convert it into a list:

tuple_nums = (1, 2, 3, 4, 5)
list_nums = list(tuple_nums)
print(list_nums)
Copy after login

The output result is: [1, 2, 3 , 4, 5]

We can also convert a set into a list. A set is an unordered collection of non-repeating elements. For example, we have a set {1, 2, 3, 4, 5}, we can use the list() function to convert it into a list:

set_nums = {1, 2, 3, 4, 5}
list_nums = list(set_nums)
print(list_nums)
Copy after login

The output result is: [1, 2, 3, 4, 5]

Finally, we can convert a dictionary's keys or values ​​into a list. A dictionary is a data structure that stores data using key-value pairs. For example, we have a dictionary {"name": "John", "age": 25, "city": "New York"}, we can use the list() function to convert its keys into a list:

dict_info = {"name": "John", "age": 25, "city": "New York"}
list_keys = list(dict_info.keys())
print(list_keys)
Copy after login

The output result is: ['name', 'age', 'city']

Similarly, we can use the list() function to convert the dictionary value into a list:

list_values = list(dict_info.values())
print(list_values)
Copy after login

The output result is: ['John', 25, 'New York']

In addition to the above examples, we can also use the list() function to combine other iterable objects such as file objects, generators, and intervals Object etc. converted to a list.

To summarize, the list() function in Python is a very convenient function that can convert other iterable objects into a list. Through this function, we can quickly and easily create lists and perform subsequent operations and processing.

The above is an introduction to how to use the list() function to create a list. I hope it will be helpful to you when using lists in Python. Happy programming!

The above is the detailed content of How to use the list() function to create a list in Python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template