Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Python List (Lists)


May 10, 2021 Python2


Table of contents


Python list (Lists).

Sequences are the most basic data structures in Python. Each element in the sequence is assigned a number - its position, or index, the first index is 0, the second index is 1, and so on.

Python has built-in types of 6 sequences, but the most common are lists and metagroups.

The actions that a sequence can perform include indexing, slicing, addition, multiplying, and checking members.

In addition, Python has built-in methods for determining the length of the sequence and determining the largest and smallest elements.

Lists are the most commonly used Python data types and can appear as comma-separated values within square brackets.

The list's data items do not need to have the same type

Create a list by enclosing the different data items separated by commas in square brackets. Here's what it looks like:

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

Like the string index, the list index starts at 0. Lists can be intercepted, combined, and so on.


Access the values in the list

Use the underseed index to access the values in the list, and you can also use square brackets to intercept characters, as follows:

#!/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]

The above example output results:

list1[0]:  physics
list2[1:5]:  [2, 3, 4, 5]

Update the list

You can modify or update the list's data items, or you can use the append() method to add list items, as follows:

#!/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];

Note: We'll discuss the use of the append() method in the next section

The above example output results:

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

Delete the list element

You can use the del statement to remove elements from a list, as 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;

The above example output results:

['physics', 'chemistry', 1997, 2000]
After deleting value at index 2 :
['physics', 'chemistry', 2000]

Note: We'll discuss the use of the remove() method in the next section


Python list script operator

The operators of the list pairs of , and , are similar to strings. The number is used to combine the list, and the number is used to repeat the list.

Here's what it looks like:

Python expression Results Describe
len([1, 2, 3]) 3 Length
[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 Iteration

Python list interception

Python's list is intercepted with the string action type, as follows:

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

Operation:

Python expression Results Describe
L[2] 'SPAM!' Read the third element in the list
L[-2] 'Spam' Read the penultimate element in the list
L[1:] ['Spam', 'SPAM!'] Start with the second element to intercept the list

Python list functions and methods

Python contains the following functions:

Serial number Function
1 cmp(list1, list2)
Compare the elements of two lists
2 len(list)
The number of list elements
3 max(list)
Returns the maximum value of the list element
4 min(list)
Returns the minimum value of the list element
5 list(seq)
Convert the yuan to a list

Python contains the following methods:

Serial number Method
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 (expand the original list with a new list)
4 list.index(obj)
Find the index location of the first match of a value from 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 last element by default) and returns the value of that element
7 list.remove(obj)
Removes the first match for a value in the list
8 list.reverse()
The element in the reverse list
9 list.sort([func])
Sort the original list

Instance:

If the Python list does not output Chinese characters properly, you can do the following:

#encoding=utf-8

import json

list_words = [ '你', '我', '他' ]
print( list_words )                                        # 无法正常显示汉字
print( str(list_words).decode( 'string_escape' ) )         # 正常显示汉字

list_words_result = json.dumps( list_words, encoding='UTF-8', ensure_ascii=False )
print( list_words_result )

The above example output results:

['\xe4\xbd\xa0', '\xe6\x88\x91', '\xe4\xbb\x96']
['你', '我', '他']
["你", "我", "他"]