Python getting started sample series 13 list

Posted by xylex on Thu, 20 Jan 2022 14:41:13 +0100

Python getting started sample series 13 list

 

sequence

Sequence is the most basic data structure in Python.

Python contains sequences built into 6, namely lists, tuples, strings, Unicode strings, buffer objects, and xrange objects. The common operations of sequences include: index, length, combination (sequence addition), repetition (multiplication), fragmentation, checking members, traversal, minimum and maximum.

Each value in the sequence has a corresponding position value, which is called an index. The first index is 0, the second index is 1, and so on.

Python has six built-in types of sequences, but the most common are strings, lists, and tuples.

 

list

List is the most commonly used Python data type and can appear as a comma separated value within square brackets.

The data items of the list do not need to have the same type.

The list can perform operations, including indexing, slicing, adding, multiplying, and checking members.

To create a list, just enclose the different data items separated by commas in square brackets. As follows:

list1 = []   # Create an empty list
list2 = [1, 2, 3]   # Create a list of 3 numbers
list3 = ['AA', 'BB', 'CC']   # Create a list of 3 strings
list4 = [1,  'AA',  (1, 2, 3),  {'URL': 'emanlee.cnblogs.com'}]   # Containing multiple data types list
list5 = [ [1,2], [3,4], [5, 6] ]   # Create a nested list

 

Gets the element (value) in the list

The forward index starts at 0 and the reverse index starts at - 1.

list3 = ['AA', 'BB', 'CC']   # Create a list of 3 strings

 

list3 First item Item 2 Item 3
List item AA  BB CC
Forward index 0  1 2
Reverse index -3 -2 -1
>>> list3 = ['AA', 'BB', 'CC']
>>> list3[0]
'AA'
>>> list3[-1]
'CC'
>>> list3[0:2]
['AA', 'BB']
>>> list3[1:]
['BB', 'CC']

 

Add element

list.append(obj) adds a new object at the end of the list.

The operators for list pairs + and * are similar to strings+ No. is used for combined list and * is used for repeated list.

>>> newlist=[1,2]+[3,4]
>>> newlist
[1, 2, 3, 4]
>>> x=[1,2]
>>> x.append([7,8])
>>> x
[1, 2, [7, 8]]
>>> [1,2]*3
[1, 2, 1, 2, 1, 2]

 

Delete element

The del statement can be used to delete the elements of the list, as shown in the following example:

>>> lst1=[1,2,3]
>>> del lst1[1]
>>> lst1
[1, 3]

 

 

Modify element

>>> lst=[1,2,3]
>>> lst[1]=4
>>> lst
[1, 4, 3]

 

 

Common methods and functions

 

Python contains the following functions:

Serial numberfunctionExample
1 len(list)
Number of list elements
 
>>> x=[1,2,3]
>>> len(x)
3

 

2 max(list)
Returns the maximum value of a list element
 
>>> x=[1,2,3]
>>> max(x)
3

 

3 min(list)
Returns the minimum value of a list element
 
>>> x=[1,2,3]
>>> min(x)
1

 

4 list(seq)
Convert seq to list
 
>>> list((1,2,3))
[1, 2, 3]
>>> list('123')
['1', '2', '3']

 

Python includes the following methods:

 

Serial numbermethodExample
1 list.append(obj)
Add a new object at the end of the list
 
>>> x=[1,2]
>>> x.append(3)
>>> x
[1, 2, 3]

 

2 list.count(obj)
Counts the number of times an element appears in the list
 
>>> x=[1,2,2,2]
>>> x.count(2)
3

 

3 list.extend(seq)
Append multiple values in another sequence at the end of the list at one time (expand the original list with the new list)
 
>>> x=[1,2]
>>> x.extend([3,4])
>>> x
[1, 2, 3, 4]

 

4 list.index(obj)
Find the index position of the first match of a value from the list
 
>>> x=[1,2,3]
>>> x.index(2)
1

 

5 list.insert(index, obj)
Insert object into list
 
>>> x=[1,2,3]
>>> x.insert(1,5)
>>> x
[1, 5, 2, 3]

 

6 list.pop([index=-1])
Removes an element from the list (the default last element) and returns the value of that element
 
>>> x=[1,2,3]
>>> x.pop()
3
>>> x
[1, 2]

 

7 list.remove(obj)
Removes the first occurrence of a value in the list
 
>>> x=[1,2,3]
>>> x.remove(1)
>>> x
[2, 3]

 

8 list.reverse()
Elements in reverse list
 
>>> x=[1,2,3]
>>> x.reverse()
>>> x
[3, 2, 1]

 

9 list.sort( key=None, reverse=False)
Sort the original list
 
>>> x=[2,1,3]
>>> x.sort()
>>> x
[1, 2, 3]

 

10 list.clear()
clear list
 
>>> x=[1,2,3]
>>> x.clear()
>>> x
[]

 

11 list.copy()
Copy list
 
>>> x=[1,2,3]
>>> x.copy()
[1, 2, 3]

 

 

 

 

 

REF

https://www.runoob.com/python3/python3-list.html

https://www.cnpython.com/list/