Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report