11.2.3.3 apply functions and methods of processing lists
Python. Lists. Functions and methods
A list is an ordered data structure, a mutable collection of elements that can contain values of different types.
Functions/Methods of lists.
Notation:
lists: lst, lst1, lst2, nums
list item: x
index: i
integer: n
Function/Method |
Example |
Description |
x in lst
|
lst = [2, 3, 4]
print(3 in lst) # True
print(5 in lst) # False
|
Check the contents of the value x in the lst list |
x not in lst |
print(3 not in [2, 3, 4]) # False
print(5 not in [2, 3, 4]) # True
|
Check that x is not in lst |
lst1 + lst2
lst1 += lst2 |
lst1 = [1, 2]
lst2 = [3, 4]
print(lst1 + lst2) # [1, 2, 3, 4]
|
Concatenation of lists. The new list contains elements sequentially first of the first list lst1, and then of the second lst2 |
lst * n
lst *= n
|
lst = [2, 3]
print(lst * 3) # [2, 3, 2, 3, 2, 3]
|
List lst repeated n times |
lst[i] |
lst = ['A', 'B', 'C', 'D']
print(lst[1]) # B
print(lst[-2]) # C
|
i-th element of the list,
a negative index counts starting from the last element |
lst[start:stop:step] |
lst = ['A', 'B', 'C', 'D']
print(lst[1:3]) # ['B', 'C']
print(lst[::2]) # ['A', 'C']
|
List slicing |
len(lst) |
lst = ['A', 'B', 'C', 'D']
print(len(lst)) # 4 |
List length (number of elements in the list) |
max(lst) |
nums = [9, 7, 4, 6]
print(max(nums)) # 9 |
Maximum list element (for numeric values) |
min(lst) |
nums = [9, 7, 4, 6]
print(min(nums)) # 4 |
Minimum list element (for numeric values) |
sum(lst) |
nums = [9, 7, 4, 6]
print(sum(nums)) # 26 |
Sum of list elements (for numeric values) |
lst.index(x) |
nums = [9, 7, 4, 6]
print(nums.index(4)) # 2 |
Index of the first occurrence of element x in the list lst
(will throw an error for if x is not in lst) |
lst.count(x) |
nums = [7, 7, 4, 6, 7]
print(nums.count(7)) # 3 |
Number of elements x in list lst |
lst.append(x)
lst.insert(i, x)
|
nums = [2, 7, 4]
nums.append(6)
print(nums) # [2, 7, 4, 6]
nums = [2, 7, 4]
nums.insert(1, 6)
print(nums) # [2, 6, 7, 4]
nums.insert(10, 3)
print(nums) # [2, 6, 7, 4, 3]
|
Add x to the end of the lst list
Insert element x at position lst[i].
Elements to the right of the insertion change index to i + 1
|
lst1.extend(lst2) |
lst1 = [2, 7]
lst2 = [4, 6]
lst1.extend(lst2)
print(lst1) # [2, 7, 4, 6] |
The elements of the list lst2 are added to the end of the list lst1, i.e. list expansion lst1 |
del lst[i] |
nums = [9, 7, 4, 6]
del nums[1]
print(nums) # [9, 4, 6] |
Removes the i-th element of the list |
del lst[start:stop:step] |
nums = [9, 7, 4, 6]
del nums[:2]
print(nums) # [4, 6] |
Removing all elements from the lst list that fall into the slice |
lst.clear() |
nums = [9, 7, 4, 6]
nums.clear()
print(nums) # [] |
Remove all elements from lst
(same as del lst[:])
|
lst.copy() |
nums = [9, 7, 4, 6]
lst = nums.copy()
print(lst) # [9, 7, 4, 6] |
Copy of list
(same as lst[:]) |
lst.remove(x) |
nums = [9, 7, 4, 6]
nums.remove(7)
print(nums) # [9, 4, 6] |
Delete the first occurrence of x in lst,
in case x not in lst - an error |
lst.pop(i) |
nums = [9, 7, 4, 6]
nums.pop(2)
print(nums) # [9, 7, 6]
nums.pop()
print(nums) # [9, 7]
|
Get the ith element of the list and remove it from the list at the same time.
With no arguments, removes the last element:
a.pop () == a.pop (-1) |
lst.reverse() |
nums = [9, 7, 4, 6]
nums.reverse()
print(nums) # [6, 4, 7, 9] |
Reverse the order of elements in a (reverse the list) |
lst.sort() |
nums = [9, 7, 4, 6]
nums.sort()
print(nums) # [4, 6, 7, 9] |
Sort the list in ascending order |
lst.sort(reverse=True) |
nums = [9, 7, 4, 6]
nums.sort(reverse=True)
print(nums) # [9, 7, 6, 4] |
Sort the list in descending order |
bool(lst) |
lst = ['apple']
print(bool(lst)) # True
lst.pop()
print(bool(lst)) # False |
Checking a list for emptiness |
Questions:
- What is the data structure 'list'?
- Provide features of the data structure 'list'?
- Name three functions of the data structure 'list'.
- Name three methods of the data structure 'list'.
Exercises:
Ex.1 Identify result of outputs (Designed by Mr. Halil Mali - CS teacher of NIS Uralsk)
Ex.2 Identify result of outputs (Designed by Mr. Halil Mali - CS teacher of NIS Uralsk)
Ex.3 Identify result of outputs (Designed by Mr. Halil Mali - CS teacher of NIS Uralsk)
Ex.4 Identify result of outputs (Designed by Mr. Halil Mali - CS teacher of NIS Uralsk)
Tasks:
|