*Memo:
- My post explains a list and the list with indexing.
- My post explains the list with slicing and copy.
- My post explains list functions (2).
append() can add a value to the end of the list as shown below:
*Memo:
- The 1st argument is
object
(Required-Type:Any):- An object is added as it is.
- Don't use
object=
.
v = ['A', 'B']
v.append('C')
print(v)
# ['A', 'B', 'C']
v.append(['D', 'E'])
print(v)
# ['A', 'B', 'C', ['D', 'E']]
v.append([])
print(v)
# ['A', 'B', 'C', ['D', 'E'], []]
extend() can add an iterable to the end of the list as shown below:
*Memo:
- The 1st argument is
iterable
(Required-Iterable):- The iterable unpacked is added.
- Don't use
iterable=
.
v = ['A', 'B']
v.extend('C')
print(v)
# ['A', 'B', 'C']
v.extend(['D', 'E'])
print(v)
# ['A', 'B', 'C', 'D', 'E']
v.extend([['F', 'G']])
print(v)
# ['A', 'B', 'C', 'D', 'E', ['F', 'G']]
v.extend([])
print(v)
# ['A', 'B', 'C', 'D', 'E', ['F', 'G']]
v.extend([[]])
print(v)
# ['A', 'B', 'C', 'D', 'E', ['F', 'G'], []]
insert() can add a value to the selected index in the list as shown below:
*Memo:
- The 1st argument is
index
(Required-Type:int
):- Don't use
index=
.
- Don't use
- The 2nd argument is
object
(Required-Type:Any):- Don't use
object=
.
- Don't use
v = ['a', 'b']
v.insert(1, 'X')
print(v)
# ['a', 'X', 'b']
v.insert(0, ['Y', 'Z'])
print(v)
# [['Y', 'Z'], 'a', 'X', 'b']
v.insert(3, [])
print(v)
# [['Y', 'Z'], 'a', 'X', [], 'b']
remove() can remove the 1st element matched to value
from the list, searching from the left to the right in the list as shown below:
*Memo:
- The 1st argument is
value
(Required-Type:Any):- Don't use
value=
.
- Don't use
- Error occurs if
value
doesn't exist.
v = ['A', 'B', 'C', 'D', 'E', ['F', 'G', 'H'], ['I', 'J']]
v.remove('C')
print(v)
# ['A', 'B', 'D', 'E', ['F', 'G', 'H'], ['I', 'J']]
# v.remove('F')
# v.remove(['F'])
# v.remove(['F', 'G'])
# ValueError: list.remove(x): x not in list
v.remove(['F', 'G', 'H'])
print(v)
# ['A', 'B', 'D', 'E', ['I', 'J']]
v[4].remove('I')
print(v)
# ['A', 'B', 'D', 'E', ['J']]
v[4].remove('J')
print(v)
# ['A', 'B', 'D', 'E', []]
v.remove([])
print(v)
# ['A', 'B', 'D', 'E']
clear() can remove all elements from the list as shown below:
*Memo:
- It has no arguments:
v = ['A', 'B', 'C', 'D', 'E', ['F', 'G', 'H'], ['I', 'J']]
v[5].clear()
print(v)
# ['A', 'B', 'C', 'D', 'E', [], ['I', 'J']]
v[6].clear()
print(v)
# ['A', 'B', 'C', 'D', 'E', [], []]
v.clear()
print(v)
# []
pop() can remove and throw the element selected by index
from the list as shown below:
*Memo:
- The 1st argument is
index
(Required-Default:-1
-Type:int
):-
-1
means the last index. - Don't use
index=
.
-
v = ['A', 'B', 'C', 'D', 'E', ['F', 'G', 'H'], ['I', 'J']]
print(v.pop(2)) # C
print(v) # ['A', 'B', 'D', 'E', ['F', 'G', 'H'], ['I', 'J']]
print(v.pop(4)) # ['F', 'G', 'H']
print(v) # ['A', 'B', 'D', 'E', ['I', 'J']]
print(v[4].pop(0)) # I
print(v) # ['A', 'B', 'D', 'E', ['J']]
print(v[4].pop(0)) # J
print(v) # ['A', 'B', 'D', 'E', []]
print(v.pop(4)) # []
print(v) # ['A', 'B', 'D', 'E']
index() can get the 1st index of the element matched to value
from the list between [start, end)
, searching from the left to the right in the list as shown below:
*Memo:
- The 1st argument is
value
(Required-Type:Any). - The 2nd argument is
start
(Optional-Default:0
-Type:int
):- It's a start index(inclusive).
- Don't use
start=
.
- The 3rd argument is
end
(Optional-Default:9223372036854775807
-Type:int
):- It's an end index(exclusive).
- Don't use
end=
.
- Error occurs if
value
doesn't exist.
v = ['A', 'B', 'C', 'D', 'A', 'B', 'C', 'D']
print(v.index('B')) # 1
print(v.index('B', 0, 9223372036854775807)) # 1
print(v.index('B', 2)) # 5
print(v.index('B', 2, 6)) # 5
print(v.index('B', 2, 5)) # ValueError: 'B' is not in list
count() can count the elements matched to value
in the list as shown below:
*Memo:
- The 1st argument is
value
(Required-Type:Any):- Don't use
value=
.
- Don't use
v = ['A', 'B', 'C', 'A', 'B', 'B', 'A', 'B']
print(v.count('A')) # 3
print(v.count('B')) # 4
print(v.count('C')) # 1
print(v.count('a')) # 0
print(v.count('D')) # 0
Top comments (0)