*Memos:
My post explains how to create a list and access and change a list by indexing.
My post explains how to access and change a list by slicing and explains shallow copy by slicing or 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 a value to the end of a list:
*Memos:
- The 1st argument is
x(Required) for a value. - A value 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'], []]
You can use extend() to add a value to the end of a list:
*Memos:
- The 1st argument is
iterable(Required) for a value. - A value 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'], []]
You can use insert() to add a value to selected position in a list:
*Memos:
- The 1st argument is
i(Required) for an index. - The 2nd argument is
x(Required) for a value. - Don't use
i=andx=.
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']
You can use remove() to remove a value from a list:
*Memos:
- The 1st argument is
x(Required) for a value. - If the value 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']
You can use a del statement to remove a value 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
You can use clear() to remove all values 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) # []
You can use pop() to remove and throw a value 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']
You can use index() to get the index of a value 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 value doesn't exist, there is error.
- Don't use
x=,start=andend=.
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
You can use count() to count how many selected values there are in a list as shown below:
*Memos:
- The 1st argument is
x(Required) for a value. - *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('d')) # 0
SOCIAL SHARE CARD GENERATOR