*Memo:
- My post explains list functions (2).
- My post explains list functions (3).
- My post explains a list (1).
index() can get the 1st index of the element matched to value from the list in the range [start, end) 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
valuedoesn't exist. -
startandendcan be signed indices(zero and positive and negative indices). - Error doesn't occur even if
[start, end)is out of the range[The 1st index, The list length).
v = ['A', 'B', 'C', 'D', 'A', 'B', 'C', 'D']
print(v.index('B'))
print(v.index('B', 0, 9223372036854775807))
print(v.index('B', -9223372036854775808, -1))
print(v.index('B', -9223372036854775808, 9223372036854775807))
# 1
print(v.index('B', 2))
print(v.index('B', -6))
print(v.index('B', 2, 6))
print(v.index('B', -6, -2))
# 5
print(v.index('B', 9223372036854775807, -9223372036854775808))
print(v.index('B', 2, 5))
print(v.index('B', 6, -3))
# 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
append() can add a value to the index of the list length in 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 values to the indices after the last index in the list as shown below:
*Memo:
- The 1st argument is
iterable(Required-Iterable):- The iterable unpacked is added.
- Don't use
iterable=.
-
+=can doextend().
v = ['A', 'B']
v.extend('C')
# v += 'C'
print(v)
# ['A', 'B', 'C']
v.extend(['D', 'E'])
# v += ['D', 'E']
print(v)
# ['A', 'B', 'C', 'D', 'E']
v.extend([['F', 'G']])
# v += [['F', 'G']]
print(v)
# ['A', 'B', 'C', 'D', 'E', ['F', 'G']]
v.extend([])
# v += []
print(v)
# ['A', 'B', 'C', 'D', 'E', ['F', 'G']]
v.extend([[]])
# v += [[]]
print(v)
# ['A', 'B', 'C', 'D', 'E', ['F', 'G'], []]
insert() can add a value to index in the list in the range [The 1st index, The list length] 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
-
indexcan be signed indices(zero and positive and negative indices). - If the list isn't empty and
indexis out of range,objectis added to the 1st index or the index of the list length without error:- If out-of-range
indexis the closest to the 1st index,objectis added to it. - If out-of-range
indexis the closest to the index of the list length,objectis added to it.
- If out-of-range
- If the list is empty and
indexis out of range,objectis added to the index0(-1) without error.
v = ['a', 'b']
v.insert(1, 'X')
# v.insert(-1, 'X')
print(v)
# ['a', 'X', 'b']
v.insert(0, ['Y', 'Z'])
# v.insert(-3, ['Y', 'Z'])
# v.insert(-100, ['Y', 'Z'])
print(v)
# [['Y', 'Z'], 'a', 'X', 'b']
v.insert(4, [])
# v.insert(100, [])
print(v)
# [['Y', 'Z'], 'a', 'X', 'b', []]
v = []
v.insert(0, 'X')
# v.insert(-1, 'X')
# v.insert(-100, 'X')
# v.insert(100, 'X')
print(v)
# ['X']
Top comments (0)