Python List of List operation method

  • 2020-04-02 13:27:54
  • OfStack

Lists are the most basic data structure in Python, lists are the most commonly used Python data types, and list items do not need to have the same type. Each element in the list is assigned a number - its position, or index, 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 a sequence can perform include indexing, slicing, adding, multiplying, and checking members. In addition, Python has built-in methods for determining the length of a sequence and determining the largest and smallest elements.

Create a list
Simply enclose the different data items separated by commas in square brackets. As follows:

list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5 ];
list3 = ["a", "b", "c", "d"];

Like the index of a string, the list index starts at 0. Lists can be intercepted, combined, and so on.
Access the values in the list
Use subscript indexes to access the values in the list, but you can also use square brackets to capture characters, as shown below:
#!/usr/bin/python

list1 = ['physics', 'chemistry', 1997, 2000];
list2 = [1, 2, 3, 4, 5, 6, 7 ];
print "list1[0]: ", list1[0]
print "list2[1:5]: ", list2[1:5]

Output results of the above examples:
list1[0]:  physics
list2[1:5]:  [2, 3, 4, 5]

Update the list
You can modify or update the list items, or you can use the append() method to add the list items, as shown below:
#!/usr/bin/python
list = ['physics', 'chemistry', 1997, 2000];
print "Value available at index 2 : "
print list[2];
list[2] = 2001;
print "New value available at index 2 : "
print list[2];

Output results of the above examples:

Value available at index 2 :
1997
New value available at index 2 :
2001

Delete list elements
The del statement can be used to delete elements from a list, as shown in the following example:
#!/usr/bin/python
list1 = ['physics', 'chemistry', 1997, 2000];
print list1;
del list1[2];
print "After deleting value at index 2 : "
print list1;

Output results of the above examples:
['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]

Python list script operators
The list operators for + and * are similar to strings. The + sign is used for combining lists, and the * sign is used for repeating lists.

As follows:

Python expression The results of describe Len ([1, 2, 3]) 3 The length of the [1, 2, 3] + [4, 5, 6] [1, 2, 3, 4, 5, 6] combination 'Hi!' * 4 [' Hi! ', 'Hi!', 'Hi!', 'Hi!] repeat 3 in [1, 2, 3] True, Whether the element exists in the list For x in [1, 2, 3]: print x, 1 2 3 The iteration
6. Python list interception
Python's list intercept and string operation types are as follows:

L = ['spam', 'Spam', 'SPAM!']

Operation:
Python expression The results of describe L [2] 'SPAM! ' Read the third element in the list L [2] 'Spam' Reads the penultimate element in the list L [1] [' Spam ', 'Spam!] Start with the second element to intercept the list
Functions and methods for list operations in Python
The list operation contains the following functions:
1. CMP (list1, list2) : compare the elements of two lists
2. Len (list) : number of elements in the list
3. Max (list) : returns the maximum value of list elements
4. Min (list) : returns the minimum value of list elements
5. List (seq) : converts tuples into lists
The list operation contains the following methods:
1. List.append (obj) : add a new object at the end of the list
2. List. Count (obj) : count the number of times an element appears in the list
3. List.extend (seq) : append multiple values from another sequence at the end of the list at one time (extend the original list with the new list)
4. List.index (obj) : find the index position of the first matching item of a value in the list
5, list.insert(index, obj) : insert the object into the list
6. List.pop (obj=list[-1]) : removes an element from the list (the default last element) and returns the value of that element
7. List.remove (obj) : removes the first match of a value in the list
8. List.reverse () : an element in an inverted list
List.sort ([func]) : sort the original list


Related articles: