Home >Backend Development >Python Tutorial >What is the meaning of list in Python? Understand the methods and usage of lists in Python in one article
List in Python is a common data type in python. It is a built-in class in python and inherits from object. Next, we will comprehensively introduce the common methods of list and the class that implements the function of classPython list
define list
>>> li = ["a", "b", "mpilgrim", "z", "example"] >>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[0] 'a' >>> li[4] 'example'
Negative list index
>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[-1] 'example' >>> li[-3] 'mpilgrim' >>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li[1:3] ['b', 'mpilgrim'] >>> li[1:-1] ['b', 'mpilgrim', 'z'] >>> li[0:3] ['a', 'b', 'mpilgrim']
Add element to list
>>> li ['a', 'b', 'mpilgrim', 'z', 'example'] >>> li.append("new") >>> li ['a', 'b', 'mpilgrim', 'z', 'example', 'new'] >>> li.insert(2, "new") >>> li ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new'] >>> li.extend(["two", "elements"]) >>> li ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements']
Traverse
list1 = [x for x in range(0,10,2)] # 方法一 for i in range(len(list1)): print(list1[i], end=' ') # 方法二 for x in list1: print(x, end=' ') # 方法三 for ind,value in enumerate(list1): print(ind, value, sep='=', end = ' ')
The above is my personal understanding. If there are any misunderstandings or errors, please correct me
The above is the detailed content of What is the meaning of list in Python? Understand the methods and usage of lists in Python in one article. For more information, please follow other related articles on the PHP Chinese website!