Python List
Sequences are the most basic data structure in Python.
Each value in a sequence has a corresponding positional value, called an index, where the first index is 0, the second index is 1, and so on. Python has six built-in types for sequences, but the most common are lists and tuples.
The operations that can all be performed on lists include indexing, slicing, adding, multiplying, and checking members.
In addition, Python has built-in methods for determining the length of a sequence and for determining the largest and smallest elements.
A list is the most commonly used Python datatype, and it can appear as a comma-separated value inside square brackets.
The data items of a list do not need to have the same type to create a list, just enclose the different data items separated by commas using square brackets.
As shown in the code below:
list1 = ['Google', 'Runoob', 1997, 2000] list2 = [1, 2, 3, 4, 5 ] list3 = ["a", "b", "c", "d"] list4 = ['red', 'green', 'blue', 'yellow', 'white', 'black']
Accessing values in a list
As with string indexes, list indexes start at 0, the second index is 1, and so on.
The index list allows you to perform interception, combination, and other operations.
list = ['red', 'green', 'blue', 'yellow', 'white', 'black'] print( list[0] ) print( list[1] ) print( list[2] )
The above example outputs the results:
red
green
blue
Indexing can also start at the end, with the last element having an index of -1, one place forward -2, and so on.
list = ['red', 'green', 'blue', 'yellow', 'white', 'black'] print( list[-1] ) print( list[-2] ) print( list[-3] )
The above example outputs the results:
black
white
yellow
Use the subscript index to access the values in the list, and you can also use the square bracket [ ] form to intercept characters, as shown below:
nums = [10, 20, 30, 40, 50, 60, 70, 80, 90] print(nums[0:4])
The above example outputs the results:
[10, 20, 30, 40]
Intercepts using negative index values:
list = ['Google', 'Runoob', "Zhihu", "Taobao", "Wiki"] # Read the second second digit print ("list[1]: ", list[1]) # Intercept from the second digit (inclusive) to the penultimate digit (exclusive) print ("list[1:-2]: ", list[1:-2])
The above example outputs the results:
list[1]: Runoob
list[1:-2]: ['Runoob', 'Zhihu']
Update List
You can modify or update the data items of a list, and you can add list items using the append() method, as shown below:
list = ['Google', 'Runoob', 1997, 2000] print ("The third element is : ", list[2]) list[2] = 2001 print ("The updated third element is : ", list[2]) list1 = ['Google', 'Runoob', 'Taobao'] ('Baidu') print ("Updated listings : ", list1)
The above example outputs the results:
The third element is : 1997
The updated third element is : 2001
Updated Listings : ['Google', 'Runoob', 'Taobao', 'Baidu']
Deleting List Elements
You can use the del statement to remove elements from a list, as in the following example:
list = ['Google', 'Runoob', 1997, 2000] print ("Original list : ", list) del list[2] print ("Delete the third element : ", list)
The above example outputs the results:
Original list : ['Google', 'Runoob', 1997, 2000]
Delete the third element : ['Google', 'Runoob', 2000]
Python List Script Operators
The list pairs + and * operators are similar to strings, with the + sign being used for combining lists and the * sign being used for repeating lists.
As shown below:
Python expression | in the end | descriptive |
len([1, 2, 3]) | 3 | lengths |
[1, 2, 3] + [4, 5, 6] | [1, 2, 3, 4, 5, 6] | combinatorial |
['Hi!'] * 4 | ['Hi!', 'Hi!', 'Hi!', 'Hi!'] | repeatable |
3 in [1, 2, 3] | True | Whether the element exists in the list |
for x in [1, 2, 3]: print(x, end=" ") | 1 2 3 | iteration (math.) |
Python List Intercept and Splice
Python's list interception and string manipulation types, as shown below:
L=['Google', 'Runoob', 'Taobao']
Operation:
Python expression | in the end | descriptive |
L[2] | 'Taobao' | Read the third element |
L[-2] | 'Runoob' | Reads the penultimate element from the right-hand side |
L[1:] | ['Runoob', 'Taobao'] | Outputs all elements after the second element |
L=['Google', 'Runoob', 'Taobao'] L[2] 'Taobao' L[-2] 'Runoob' L[1:] ['Runoob', 'Taobao']
The list also supports splice operations:
>>> squares = [1, 4, 9, 16, 25] >>> squares += [36, 49, 64, 81, 100] >>> squares [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
nested list
Using nested lists i.e. creating other lists within a list, for example:
>>> a = ['a', 'b', 'c'] >>> n = [1, 2, 3] >>> x = [a, n] >>> x [['a', 'b', 'c'], [1, 2, 3]] >>> x[0] ['a', 'b', 'c'] >>> x[0][1] 'b'
Python list functions & methods
The Python list contains the following functions.
0 | function (math.) |
1 | len(list) number of list elements |
2 | max(list) Returns the maximum value of the list elements |
3 | min(list) Returns the minimum value of a list element |
4 | list(seq) Converts a tuple to a list. |
The Python list contains the following methods.
- (obj) Adds a new object to the end of the list.
- (obj) counts the number of times an element appears in a list
- (seq) Append multiple values from another sequence to the end of the list in one go (extend the original list with a new list).
- (obj) find the index position of the first match for a value from the list
- (index, obj) Insert object into list
- ([index=-1]) Removes an element from the list (the last element by default) and returns the value of that element.
- (obj) Remove the first match of a value in the list.
- (obj) Remove the first match of a value in the list.
- ( key=None, reverse=False) Sorts the original list.
- () Empty the list
- () Copy List
to this article on the list of Python's various methods of operation of the article is introduced to this, more relevant Python list of operations please search my previous posts or continue to browse the following related articles I hope you will support me in the future more!