Python3 list


Sequence is the most basic data structure in Python. Each element in the sequence is assigned a number - its position, or index, with the first index being 0, the second index being 1, and so on.

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

Operations that can be performed on sequences 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.

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

The data items in the list do not need to be of the same type

To create a list, just use square brackets to enclose the different data items separated by commas. It looks like this:

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

Like string indexing, list indexing starts at 0. Lists can be intercepted, combined, etc.


Access the values ​​in the list

Use subscript index to access the values ​​in the list. You can also use square brackets to intercept characters, as shown below:

#!/usr/bin/python3

list1 = ['Google', 'php', 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 result:

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

Update list

You can modify or update the data items in the list, you can also use append () method to add list items, as shown below:
#!/usr/bin/python3

list = ['Google', 'php', 1997, 2000]

print ("第三个元素为 : ", list[2])
list[2] = 2001
print ("更新后的第三个元素为 : ", list[2])

Note: We will discuss the use of the append() method in the next chapter

Output results of the above example:

第三个元素为 :  1997
更新后的第三个元素为 :  2001

Delete list elements

You can use the del statement to delete elements of the list, as shown in the following example:

#!/usr/bin/python3

list = ['Google', 'php', 1997, 2000]

print (list)
del list[2]
print ("删除第三个元素 : ", list)

The output result of the above example:

删除第三个元素 :  ['Google', 'php', 2000]

Note: We will discuss the use of the remove() method in the next chapter


Python list script operator

List operations on + and * Characters are similar to strings. The + sign is used for combined lists, and the * sign is used for repeated lists.

looks like this:

Python expressionResultDescription
len([1, 2, 3])3Length
[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]TrueWhether the element exists in the list
for x in [1, 2, 3]: print x ,1 2 3Iteration

##Python list interception and splicing

Python list interception with string operation types, as follows:

L=['Google', 'php', 'Taobao']

Operation:

Python expressionResult DescriptionL[2]'Taobao'Read the third element L[-2]'php'Read the second to last element from the right: count from the right##L[ 1:]
>>> L=['Google', 'php', 'Taobao']
>>> L[2]
'Taobao'
>>> L[-2]
'php'
>>> L[1:]
['php', 'Taobao']
>>>

The list also supports splicing operations:

>>> squares = [1, 4, 9, 16, 25]
>>> squares + [36, 49, 64, 81, 100]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

Nested list

Using nested lists means creating other lists within the 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

Python contains the following functions:

['php', 'Taobao']Output all elements starting from the second element
Serial numberFunction
1len(list)
Number of elements in the list
2max(list)
Return list Maximum value of the element
3min(list)
Returns the minimum value of the element in the list
4 list(seq)
Convert tuple to list

Python contains the following methods:

##1list.append(obj)2list.count(obj)3list.extend(seq)##4Find the index position of the first matching item of a value from the list5Insert objects into the list6Remove objects from the list An element (default last element), and returns the value of the element7Remove a value in the list The first match of 8Reverse the element in the list 9Sort the original list10 Clear the list11Copy list
Serial numberMethod
Add a new object at the end of the list
Count the number of times an element appears in the list
Append multiple values ​​from another sequence at the end of the list (extend the original list with a new list)
list.index(obj)
list.insert( index, obj)
list.pop(obj=list[-1])
list.remove(obj)
list.reverse()
list.sort([func])
list.clear()
list.copy()