Python full-stack development: python List, pythonlist
List
A list is a frequently used data type in python and is used to store multiple values.
Format: separated by commas (,) in []. You can store n values of any type.
Eg:
# Single type users = ['yanglil', 'yite ', 'fixd'] # multi-type, list nested users = ['room01', ['yanglil', 20, 'music'], ['yite ', 20, 'basketball'],]
List common methods and functions
Method
Function
Common Operations
Eg:
len(list)
# Len (list) Calculate the length of the list ls = ['A', 'B', 'C', 'D'] length = len (ls) print (length) # result: 4
append()
# Append (): append ls = ['A', 'B', 'C', 'D'] ls. append ('x') print (ls) # result: ['A', 'B', 'C', 'D ', 'X'] # append add elements to the end of the list
inser(index,obj)
# Inser (index, obj) insert: index inserts the starting index of the List (required). The elements inserted by obj are ls = ['A', 'B', 'C ', 'D'] ls. insert (0, 'x') # insert print (ls) in the header # result: ['x', 'A', 'B', 'C ', 'D'] ls = ['A', 'B', 'C', 'D'] ls. insert (len (ls), 'x') # insert print (ls) at the end # result: ['A', 'B', 'C', 'D ', 'X'] ls = ['A', 'B', 'C', 'D'] ls. insert (2, 'x') # insert objprint (ls) before the element indexed to 2 in the original list # result: ['A', 'B', 'x ', 'C', 'D']
remove()
# Remove () removes the first match of a value in the list. No return value ls = ['A', 'B', 'C', 'D'] ls. remove ('C') # Delete cprint (ls) # result: ['A', 'B', 'D']
pop(index)
# Pop (index) removes the first matching item of a value in the list. No return value ls = ['A', 'B', 'C ', 'D '] res = ls. pop () # The last element print (ls) is deleted by default. # result: ['A', 'B', 'C'] print (res) # result: d pop () returns the deleted elements ls = ['A', 'B', 'C', 'D'] res = ls. pop (2) # The last element print (ls) is deleted by default. # result: ['A', 'B', 'D'] print (res) # result: c pop () returns the deleted element.
reverse()
# Reverse () # reverse list element ls = ['A', 'B', 'C', 'D'] ls. reverse () print (ls) # ['D', 'C', 'B', 'a']
List Operators
+ And * operators are similar to strings. + Is used to combine the list, and * is used to repeat the list.
# + * Ls1 = ['A', 2, 'C'] ls2 = [1, 'y', 'z'] ls3 = ls1 + ls2print (ls3) # result: ['A', 2, 'C', 1, 'y', 'z'] print (ls1 * 3) # result: ['A', 2, 'C', 'A', 2, 'C', 'A', 2, 'C']
In is similar to the use of in strings.
# In # determine whether an element exists in the list. ls = ['A', 'B', 'C', 'D'] print ('C' in ls) # Trueprint ('x' in ls) # False
List comparison (prerequisite: The comparison can be performed only when the same type is required; otherwise, an error is returned)
# Major premise: only the sizes of the same type can be directly compared. For direct comparisons with index values, compare the values of ls1 = ['A', 'B' according to their locations ', 'C'] ls2 = ['x', 'y', 'z'] ls3 = ['A', 'z'] print (ls1> ls2) # result: falseprint (ls1> ls3) # result: True
To be supplemented...
Slice of the List
The Slice operation of the list is not described here. Please refer to the slice of the string.
Full-stack Python development: python string Slicing
Read list
Ls = ['A', 'B', 'C', 'D'] # dependent index I = 0 while I <len (ls ): # while loop print (ls [I]) I + = 1for I in range (len (ls): # for Loop print (I, ls [I]) # for item in ls: # for Loop print (item)