preface
1, What is the list
1. Access list elements
2. Index starts from 0 instead of 1
3. Use the values in the list
2, Modify, add, and delete elements
1. Modify list elements
2. Add elements to the list
3. Remove the element from the list
3, Organization list
1. Use the method sort() to permanently sort the list
2. Use the method sorted() to temporarily sort the list
3. Print the list upside down
4. Determine the length of the list
4, Avoid index errors when using lists
summary
preface
In this and the next chapter, you will learn what a list is and how to use list elements. Lists allow you to store groups of information in one place, which can contain only a few elements or millions of elements. List is one of the most powerful python functions that novices can use directly. It integrates many important programming concepts.
1, What is the list
A list is a series of elements arranged in a specific order. In Python, lists are represented by square brackets ([]) and the elements are separated by commas.
1. Access list elements
A list is an ordered collection, so to access any element in the list, just tell Python the location (index) of the element.
list = ['red', 'green', 'blue', 'yellow', 'white', 'black'] print( list[1] )
Output: green
2. Index starts from 0 instead of 1
In fact, a list is similar to an array, and the subscripts start from 0.
list = ['red', 'green', 'blue', 'yellow', 'white', 'black'] print( list[0] )
Output: red
Note: in the above example, if it is print (list[-1]), the output is black----- Fine. It will be useful later.
3. Use the values in the list
There's nothing to say about this. You can index list elements and use them.
2, Modify, add, and delete elements
1. Modify list elements
The syntax for modifying list elements is similar to that for accessing list elements. To modify a list element, specify the list name and the index of the element to be modified, and then specify the new value of the element.
motorcycles=['honda','yamaha','suzuki'] print(motorcycles) motorcycles[0]='ducati' print(motorcycles)
['honda','yamaha','suzuki'] ['ducati','yamaha','suzuki']
2. Add elements to the list
append() is used to add a new object to the end of the list. (don't care if the list is empty)
Syntax: list Append (obj) (Note: Obj -- object added to the end of the list)
list1 = ['Google', 'Runoob', 'Taobao'] list1.append('Baidu') print ("Updated list : ", list1) The output results of the example are as follows: Updated list : ['Google', 'Runoob', 'Taobao', 'Baidu']
The insert() function is used to insert the specified object into the specified position of the list.
Syntax: list Insert (index, obj) (Note: index -- index position of object obj to be inserted, obj -- object to be inserted in the list.)
list1 = ['Google', 'Runoob', 'Taobao'] list1.insert(1, 'Baidu') print ('After inserting elements into the list : ', list1) The output results of the example are as follows: After inserting elements into the list : ['Google', 'Baidu', 'Runoob', 'Taobao']
3. Remove the element from the list
Delete elements using del statements
list = ['Google', 'Runoob', 1997, 2000] print ("Original list : ", list) del list[2] print ("Delete the third element : ", list) Instance output result: Original list : ['Google', 'Runoob', 1997, 2000] Delete the third element : ['Google', 'Runoob', 2000]
Delete the element using the method pop()
The pop() function is used to remove an element in the list (the default last element) and return the value of the element. (Note: it can be used to delete any element in the list)
list1 = ['Google', 'Runoob', 'Taobao'] list_pop=list1.pop(1) print ("Deleted item is :", list_pop) print ("The list is now : ", list1) The output results of the example are as follows: The deleted element is : Runoob The list is now : ['Google', 'Taobao']
Use the method remove() to delete the element
The remove() function removes the first match of a value in the list.
ist1 = ['Google', 'Runoob', 'Taobao', 'Baidu'] list1.remove('Taobao') print ("The list is now : ", list1) list1.remove('Baidu') print ("The list is now : ", list1) The output results of the example are as follows: The list is now : ['Google', 'Runoob', 'Baidu'] The list is now : ['Google', 'Runoob']
3, Organization list
1. Use the method sort to permanently sort the list
The sort() function is used to sort the original list. If parameters are specified, the comparison function specified by the comparison function is used.
Syntax: list sort(cmp=None, key=None, reverse=False)
parameter
-
cmp -- optional parameter. If this parameter is specified, the method of this parameter will be used for sorting.
-
key -- the element mainly used for comparison. There is only one parameter. The parameters of the specific function are taken from the iteratable object. Specify an element in the iteratable object for sorting.
-
Reverse -- collation, reverse = True , descending, reverse = False , ascending (default).
aList = ['123', 'Google', 'Runoob', 'Taobao', 'Facebook']; aList.sort(); print("List : "aList) The output results of the example are as follows: List : ['123', 'Facebook', 'Google', 'Runoob', 'Taobao']
2. Use the method sorted () to temporarily sort the list
The sorted () function does not change the sorting of the elements in the list, but it will change temporarily when used.
cars = ['bmw', 'audi', 'toyota', 'subaru'] print("Here is the original list:") print(cars) print("\nHere is the sorted list:") print(sorted(cars)) print("\nHere is the original list again:") print(cars) Output: Here is the original list: ['bmw', 'audi', 'toyota', 'subaru'] Here is the sorted list: ['audi', 'bmw', 'subaru', 'toyota'] Here is the original list again: ['bmw', 'audi', 'toyota', 'subaru']
3. Print the list upside down
The reverse() function is used to reverse the elements in the list.
Syntax: list reverse()
aList = [123, 'xyz', 'zara', 'abc', 'xyz'] aList.reverse() print ("List : ", aList) The output results of the example are as follows: List : ['xyz', 'abc', 'zara', 'xyz', 123]
4. Determine the length of the list
len() function can get the length of the list
>>> cars = ['bmw', 'audi', 'toyota', 'subaru'] >>> len(cars) 4
4, Avoid index errors when using lists
This is what you should pay attention to the length and subscript of the list.
summary
In this chapter, you learned: what is a list and how to use its elements; How to define a list and how to add or delete elements; How to permanently sort the list and how to temporarily sort the list for display; How to determine the length of the list and how to avoid index errors when using the list.