Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use list (), the constructor of python list

Shulou Source: shulou.com Published: 2022-06-01 06:47:57 10月03日 Update

This article mainly introduces the "python list construction method list () how to use" related knowledge, editor through the actual case to show you the operation process, the method of operation is simple and fast, practical, hope that this "python list construction method list () how to use" article can help you solve the problem.

First take a look at the construction of the list:

List = [element1, element2, element3,..., elementn]

Lists can store data of any type, such as integers, decimals, strings, lists, tuples, and so on, and the types of elements in the same list can be different.

There are two ways to construct a list:

The first method: we can directly use brackets [] to create, directly add data in it, separated by English commas, if it is a string can be separated by single or double quotation marks, let's use a few examples to see.

List1 = [1, 2, 3, 4, 5] # element is shaping list2 = ['python',' java', 'php',' Category lists'] # element is the string list3 = [['python',' django', 'pyqt',' lxml'], 'java',' php', 'Cellular lists'] # element

Of course, the list can also contain other data types, such as collections, dictionaries, and so on. Let's not demonstrate here.

The second way: we can use the list () function to create a list indirectly.

# convert a string to a list list1 = list ("hello") print (list1) # convert a tuple to a list tuple1 = ('Python',' Java', 'cantilever,' JavaScript') list2 = list (tuple1) print (list2) # convert a dictionary into a list dict1 = {'axie: 100,' baked: 42, 'cased: 9} list3 = list (dict1) print (list3) # convert an interval into a list range1 = range (1) 6) list4 = list (range1) print (list4) # create empty list print (list ())

The returned result is as follows:

['hype,' estranged, 'lumped,' lumped,'o']

['Python',' Java', 'Clearing girls,' JavaScript']

['averse,' baked,'c']

[1, 2, 3, 4, 5]

[]

This is the end of the content about "how to use list (), the construction method of python list". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

Tags: Methods elements characters strings data knowledge types differences dictionaries quotes industries utility content functions intervals examples practicality practice decimals integers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS OPPO Reno Shulou Tech Info Linux Shulou Information