Python101: 8. List

post thumb
Python
by Admin/ on 06 Jul 2021

Python101: 8. List

A data structure in Python is a collection of data elements that are organized in some way, and these data elements can be numbers, characters, or even other data structures.

In Python, the most basic data structures are sequences (lists and tuples), where each element in the sequence has a number (the specific location of the element), which is called an index, and the index subscript starts at 0 and so on ……

  • Lists are commonly known as Python’s drudgery, and lists are mutable (you can change the contents of a list)
  • Lists are the most commonly used Python data type, and can appear as a comma-separated value inside square brackets.
  • Data items of a list need not have the same type

To create a list, simply enclose the different data items separated by commas in square brackets. This is shown below.

list1 = ['baidu', 'google', 12, 34]
list2 = [1, 2, 3, 4, 5]
list3 = ["a", "b", "c", "d" ]

1. list function


1.1 list function

If you want to change a value in a string after assigning a value to it, because strings cannot be changed like lists, you can use the list function if you want to change it, as follows.

ll=list('hello')

ll
# ['h', 'e', 'l', 'l', 'o']

ll[2]
# 'l'

ll[2]='5'

ll
# ['h', 'e', '5', 'l', 'o']

Note: The list function works on all types of sequences, not just strings

1.2 len function

The len function returns the number of elements in the list

list1 = ['baidu', 'google', 12, 23]

len(list1)
# 4

1.3 max function

The max function returns the maximum value of the list elements

list_num=[2,3,5,6,8,12]

max(list_num)
# 12

1.4 min function

Returns the minimum value of a list element

list_num=[2,3,5,6,8,12]

min(list_num)
# 2

2 List method


The list provides several detailed methods that are used to check or modify the contents of the list

2.1 append

The append method is used to append new content to the end of the list

list_append = [1,2,3,4]
list_append.append(5)
list_append
# [1, 2, 3, 4, 5]

2.2 count

The count method is used to count the number of times an element appears in the list

num = [1, 2, 3, 4, 5, 5, 5, 5, 6]

# Count the number of occurrences of 5 in the num list
num.count(5)
3 4

# Count the number of times the letter a appears
name=['a','a','abf','ark','nhk']

name.count('a')
# 2

2.3 extend

The extend method represents appending, it can append multiple values from another sequence at once at the end of the list, i.e. extending the original list with a new list

a = [1,2,3]
b = [4,5,6]

# Append list b after list a
a.extend(b)

a
# [1, 2, 3, 4, 5, 6]

This operation is the same as the add operation of a list, but the append operation changes the original list, while the add does not change the original list, e.g.

a = [1,2,3]
b = [4,5,6]

a + b
# [1, 2, 3, 4, 5, 6]

# The join operation does not change the original list
a
# [1, 2, 3]

2.4 index

The index method is used to find the index position of the first matching position of an element from the list

content = ['where','who','lisi','cntent','who']
content.index('who')
# 1

Note: The above method has ‘who’ in two positions, but only the first matching index position element is found

2.5 insert

The insert method is used to insert objects into a list

num = [1,2,5,6,7]
num.insert(2,3)

num
# [1, 2, 3, 5, 6, 7]

num.insert(3,4)

num
# [1, 2, 3, 4, 5, 6, 7]

2.6 pop

The pop method removes an element from the list (the last one by default) and returns the value of that element

x = [1,2,3]

x.pop()
# 3

x
# [1, 2]

x.pop()
# 2

x
# [1]

Note that the pop method is the only method that both modifies the list and returns the element values (except for None), and that the pop and append methods are the stacking and stacking of data structures in Python; if you append the value that just came off the stack (pop), you still get the original list

x = [1,2,3]

x.append(x.pop())
x
# [1, 2, 3]

2.7 remove

The remove method is used to remove the first matching element from the list

content = ['where', 'who', 'lisi', 'cntent', 'who', 'who']

# Remove the first matching element
content.remove('who')
content
# ['where', 'lisi', 'cntent', 'who', 'who']

2.8 reverse

The reverse method reverses the elements of the list

x = [1, 2, 3]

# elements stored in reverse
x.reverse()
x
# [3, 2, 1]

2.9 sort

The sort method is used to sort in the original position, ‘sort in original position’ means to change the original list and let the elements of the list in order

x = [2,3,5,6,1,4,7]
x.sort()
x
# [1, 2, 3, 4, 5, 6, 7]

2.10 clear

The clear method is used to clear the list

list1=['baidu', 'google', 12, 23]
list1
# ['baidu', 'google', 12, 23]

# Clear the contents of the list
list1.clear()
list1
# []

2.11 copy

The copy method is a copy of the list

list1 = ['baidu', 'google', 12, 23]
list1.copy()
# ['baidu', 'google', 12, 23]

list2 = list1.copy()
list2
# ['baidu', 'google', 12, 23]

3. List basic operations


Lists can use all the standard operations that apply to sequences, such as indexing, slicing, concatenation, and multiplication that we learned on Day 7. More interestingly, lists can be modified, that is, the contents of the defined list can be changed as needed. This section describes some methods for changing lists: element assignment, element deletion, slice assignment, and list methods (but note that not all list methods can actually change the list)

3.1 Changing lists: element assignment

To assign a value to a specified element in a list, we need to specify a specific index tag to assign a value to a specific, well-positioned element in the list, e.g. x[3]=5

x=[1,2,3,4,5]
x
# [1, 2, 3, 4, 5]

# Change the content of the fourth element of the list
x[3]=5
x
# [1, 2, 3, 5, 5]

Note: cannot assign a value to an element whose position does not exist, if the length of the list is 2, you cannot assign a value to an element whose index is 10, if you need to assign a value, you need to create a list of length 11.

3.2 Deleting list elements

To delete an element in the list, just use del to delete it

# Define a list of names of length 4
names=['zhangsan','lisi','wangwu','zhaoliu']
names
# ['zhangsan', 'lisi', 'wangwu', 'zhaoliu']

# Delete the third element
del names[2]

# Final list length changed from 4 to 3
names
# ['zhangsan', 'lisi', 'zhaoliu']

The del statement can also be used to delete other elements and can be used for variable deletion operations.

3.3 Slice assignment

Slicing operations on sequences or lists is a very powerful feature in Python, and slicing assignments can seem even more powerful, e.g.

# Define a list
name = list('Pyther')

# Change the last two values in the list
name[4:]='on'
name
# ['p', 'y', 't', 'h', 'o', 'n']

From the above, it can be seen that the program can assign values to more than one element at a time, and when assigning values in pieces, the pieces can be replaced with sequences that are not equal in length to the original sequence, e.g.

name_re = list('perl')
name_re
# ['p', 'e', 'r', 'l']

# Piecewise replacement
name_re[1:] = list('ython')
name_re
# ['p', 'y', 't', 'h', 'o', 'n']

Slice assignment also allows new elements to be inserted without changing anything in the original list

num = [1,4,5]

# Insert a new element after the first one
num[1:1]=[2,3]
num
# [1, 2, 3, 4, 5]

Similarly, you can also delete elements in the list by slicing, which also supports negative slicing

num = [1, 2, 3, 4, 5]

# Assign an empty sequence to the slice between the first and disan element, i.e. delete the element
num[1:3] = []
num
# [1, 4, 5]

# Negative slicing operation
num[-1:-1] = [5,5,5]
num
# [1, 2, 3, 4, 5, 5, 5, 5]

Summary


This section introduces you to the manipulation and use of Python data structures, and provides support for Python engineers working with lists.


Reference

https://github.com/JustDoPython/python-100-day/tree/master/day-008

comments powered by Disqus