DEV Community

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

Posted on • Edited on

List in Python (3)

Buy Me a Coffee

*Memos:

  • My post explains a list and the list with indexing.
  • My post explains the list with slicing and copy.
  • My post explains the useful functions for a list (2).
  • My post explains variable assignment.
  • My post explains shallow copy and deep copy.

You can use append() to add an element to the end of a list:
*Memos:

  • The 1st argument is x(Required) for an element.
  • An element is added as it is.
  • Don't use x=.
v = ['a', 'b', 'c']

v.append('d')
print(v) # ['a', 'b', 'c', 'd']

v.append(['e', 'f'])
print(v) # ['a', 'b', 'c', 'd', ['e', 'f']]

v.append([])
print(v) # ['a', 'b', 'c', 'd', ['e', 'f'], []]
Enter fullscreen mode Exit fullscreen mode

You can use extend() to add an element to the end of a list:
*Memos:

  • The 1st argument is iterable(Required) for an element.
  • An element is added, flattening the most outer dimension.
  • Don't use iterable=.
v = ['a', 'b', 'c']

v.extend('d')
print(v) # ['a', 'b', 'c', 'd']

v.extend(['e', 'f'])
print(v) # ['a', 'b', 'c', 'd', 'e', 'f']

v.extend([['g', 'h']])
print(v) # ['a', 'b', 'c', 'd', 'e', 'f', ['g', 'h']]

v.extend([])
print(v) # ['a', 'b', 'c', 'd', 'e', 'f', ['g', 'h']]

v.extend([[]])
print(v) # ['a', 'b', 'c', 'd', 'e', 'f', ['g', 'h'], []]
Enter fullscreen mode Exit fullscreen mode

You can use insert() to add an element to selected position in a list:
*Memos:

  • The 1st argument is i(Required) for an index.
  • The 2nd argument is x(Required) for an element.
  • Don't use i= and x=.
v = ['a', 'b', 'c', 'd']

v.insert(2, 'C')
print(v) # ['a', 'b', 'C', 'c', 'd']

v.insert(0, ['A', 'B'])
print(v) # [['A', 'B'], 'a', 'b', 'C', 'c', 'd']

v.insert(4, [])
print(v) # [['A', 'B'], 'a', 'b', 'C', [], 'c', 'd']
Enter fullscreen mode Exit fullscreen mode

You can use remove() to remove an element from a list:
*Memos:

  • The 1st argument is x(Required) for an element.
  • If the element doesn't exist, there is error.
  • Don't use x=.
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') # ValueError: list.remove(x): x not in list
v.remove(['f']) # ValueError: list.remove(x): x not in list
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']
Enter fullscreen mode Exit fullscreen mode

You can use a del statement to remove an element from a list by an index in each [] or by slice or to remove a variable itself as shown below:

v = ['a', 'b', 'c', 'd', 'e', ['f', 'g', 'h'], ['i', 'j']]

del v[2]
print(v) # ['a', 'b', 'd', 'e', ['f', 'g', 'h'], ['i', 'j']]

del v[4]
print(v) # ['a', 'b', 'd', 'e', ['i', 'j']]

del v[4][0]
print(v) # ['a', 'b', 'd', 'e', ['j']]

del v[4][0]
print(v) # ['a', 'b', 'd', 'e', []]

del v[4]
print(v) # ['a', 'b', 'd', 'e']

del v[1:3]
print(v) # ['a', 'e']

del v
print(v) # NameError: name 'v' is not defined
Enter fullscreen mode Exit fullscreen mode

You can use clear() to remove all elements from a list. *There are no arguments.

v = ['a', 'b', 'c', 'd', 'e', ['f', 'g', 'h'], ['i', 'j']]

v[5].clear()
# del v[5][:]
print(v) # ['a', 'b', 'c', 'd', 'e', [], ['i', 'j']]

v[6].clear()
# del v[6][:]
print(v) # ['a', 'b', 'c', 'd', 'e', [], []]

v.clear()
# del v[:]
print(v) # []
Enter fullscreen mode Exit fullscreen mode

You can use pop() to remove and throw an element from a list:
*Memos:

  • The 1st argument is [i](Required) for an index.
  • Don't use any keyword like [i], i, etc.
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']
Enter fullscreen mode Exit fullscreen mode

You can use index() to get the index of an element from a list as shown below:
*Memos:

  • The 1st argument is x(Required) for an index.
  • The 2nd argument is start(Optional) for the start of an index.
  • The 3rd argument is end(Optional) for the end of an index.
  • If the element doesn't exist, there is error.
  • Don't use x=, start= and end=.
v = ['a', 'b', 'c', 'd', 'a', 'b', 'c', 'd']

print(v.index('b')) # 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
Enter fullscreen mode Exit fullscreen mode

You can use count() to count how many selected elements there are in a list as shown below:
*Memos:

  • The 1st argument is x(Required) for an element.
  • *Don't use x=.
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'))
print(v.count('d'))
# 0
Enter fullscreen mode Exit fullscreen mode

Top comments (0)