The list is python One of the data types in , Keywords are list. list (list) Is a variable sequence type , We can add 、 Insert 、 Delete and Replace the elements in the list .
How to create a list :
1、 Use list function
2、 Use [] Specify a specific element list
print(list('hello world')) # ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] print([1, 3, 5, 7, 9]) # [1, 3, 5, 7, 9]
Add elements to the list :
1、 Use append() Method to add a single element
2、 Use extend() Method or operator + Batch add multiple
a = [1,2,3] print(a.append(4)) #None append Function return value is null print(a) # [1, 2, 3, 4] b = [4,5,6] a.extend(b) #None extend Function return value is null print(a) # [1, 2, 3, 4, 4, 5, 6] c = ['a','b','c'] print(a+c) # [1, 2, 3, 4, 4, 5, 6, 'a', 'b', 'c']
List inserts elements :
list.insert(index,value)
a = [1,2,3] a.insert(0, 'abcd') # insert Function has no return value print(a)
Replace list elements :
a = [1,2,3] a[0]='123' print(a)
Delete list elements :
pop() Delete the last element , This method has a return value , Returns the value of the deleted element
remove(xxx): Delete the first match in the list xxx The elements of
summary :
This article is from WeChat official account. - Xiaobo's road to growth (libotest)
The source and reprint of the original text are detailed in the text , If there is any infringement , Please contact the [email protected] Delete .
Original publication time : 2021-09-22
Participation of this paper Tencent cloud media sharing plan , You are welcome to join us , share .