DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

List functions in Python (1)

Buy Me a Coffee

*Memo:

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 value doesn't exist.
  • start and end can 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
Enter fullscreen mode Exit fullscreen mode

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=.
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
Enter fullscreen mode Exit fullscreen mode

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'], []]
Enter fullscreen mode Exit fullscreen mode

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 do extend().
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'], []]
Enter fullscreen mode Exit fullscreen mode

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=.
  • The 2nd argument is object(Required-Type:Any):
    • Don't use object=.
  • index can be signed indices(zero and positive and negative indices).
  • If the list isn't empty and index is out of range, object is added to the 1st index or the index of the list length without error:
    • If out-of-range index is the closest to the 1st index, object is added to it.
    • If out-of-range index is the closest to the index of the list length, object is added to it.
  • If the list is empty and index is out of range, object is added to the index 0(-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', []]
Enter fullscreen mode Exit fullscreen mode
v = []

v.insert(0, 'X')
# v.insert(-1, 'X')
# v.insert(-100, 'X')
# v.insert(100, 'X')

print(v)
# ['X']
Enter fullscreen mode Exit fullscreen mode

Top comments (0)