In [ ]:
L1 = ['A', 'B', 'C']
L2 = list(['A', 'B', 'C'])
L3 = list(('A', 'B', 'C'))
L4 = sorted(list({'A', 'B', 'C'}))
L5 = sorted(list({'A': 1, 'B': 2, 'C': 3}))
L6 = list('ABC')
L7 = L1.copy()
L1 == L2 == L3 == L4 == L5 == L6 == L7
In [ ]:
L = [1, 3, 5, 7, 3, 5, 5]
print(len(L))
print(0 not in L)
print(5 in L)
print(L.index(5))
print(L.count(5))
In [ ]:
L = []
L.append(3)
print(L)
L.append(9)
print(L)
L.insert(1, 7)
print(L)
L.insert(-2, 5)
print(L)
# 0 can be replaced by any negative integer
# at most equal to minus the length of L
L.insert(0, 1)
print(L)
# 5 can be replaced by any larger integer
L.insert(5, 11)
print(L)
L.append(13)
print(L)
L.extend([15])
print(L)
L.extend([17, 19])
print(L)
In [ ]:
L = [0, 2, 4, 6, 8, 0, 2, 4, 6, 8]
L.pop()
print(L)
L.pop(8)
print(L)
L.pop(-8)
print(L)
L.pop(3)
print(L)
L.remove(4)
print(L)
L.clear()
print(L)
In [ ]:
L = [2, 0, 5, 1, 4, 3]
L.sort()
print(L)
L.sort(reverse = True)
print(L)
L.reverse()
print(L)
print()
L = ['A', 'd', 'C', 'b']
L.sort()
print(L)
L.sort(key = str.upper)
print(L)
In [ ]:
L = [1] + [2, 3]
print(L)
L += [4, 5, 6]
print(L)
L = [1, 2] * 3
print(L)
L *= 2
print(L)
In [ ]:
L1 = [1]
L2 = L1
print(L2)
L2 += [2, 3]
L2 = L2 + [4, 5]
print(L2)
print(L1)
In [ ]:
L = [0, 2, 4, 6]
print(L[1])
L[1] = 1
print(L)
print(L[-2])
L[-2] = 3
print(L)
print()
# Alternatively: L[2: len(L)]
print(L[2:])
L[2:] = []
print(L)
L[2:] = [4, 5, 6]
print(L)
print(L[-2:])
L[-2:] = [1, 2, 3]
print(L)
print()
# Alternatively: L[0: 4]
print(L[: 4])
L[: 4] = [3]
print(L)
print(L[1: -1])
L[1: -1] = [2, 4, 2]
print(L)
# The second occurrence of 3 can be replaced by any smaller integer
print(L[3: 3])
L[3: 3] = [1]
print(L)
print()
# Alternatively: L[0: len(L): 2]
print(L[: : 2])
print(L[1: : 2])
L[1: : 2] = [-2, -1, -3]
print(L)
print(L[: 4: 2])
print(L[1: 5: 2])
# Alternatively: L[-1: -len(L) - 1: -2]
print(L[: : -2])
print(L[: 1: -2])
print(L[4: : -2])
print(L[4: 1: -2])
L[4: 1: -2] = [-2, -4]
print(L)
del L[0]
print(L)
del L[-3: 0: -1]
print(L)
del L[: ]
print(L)
In [ ]:
L = [[[1, 2, 3, 4], [5], [6, 7], [8, 9, 10]],
[[11, 12], [13, 14, 15, 16, 18], [19, 20]],
[[21], [22], [23, 24, 25], [26, 27], [28, 29, 30]]]
print(L[1])
print(L[1][1])
print(L[1][1][3])
print()
L1 = L
L[1][1][3] = [0]
print(L)
L[1][1] = 0
print(L)
L[1] = []
print(L)
print(L1 == L)
In [ ]:
from copy import deepcopy
L = [list(range(1, 4)), list(range(4, 7)), list(range(7, 10))]
print(L)
L1 = L.copy()
L2 = deepcopy(L)
L[0] = [0]
L[1][1] = -5
print(L)
print(L1)
print(L2)
In [ ]:
T1 = 'A', 'B', 'C'
T2 = tuple(['A', 'B', 'C'])
T3 = tuple(('A', 'B', 'C'))
T4 = tuple({'A', 'B', 'C'})
T5 = tuple({'A': 1, 'B': 2, 'C': 3})
T6 = tuple('ABC')
print(T1, T2, T3, T4, T5, T6)
In [ ]:
T = 1, 3, 5, 7, 3, 5, 5
print(len(T))
print(0 not in T)
print(5 in T)
print(T.index(5))
print(T.count(5))
In [ ]:
T = (1,) + (2, 3)
print(T)
T += (4, 5, 6)
print(T)
T = (1, 2) * 3
print(T)
T *= 2
print(T)
In [ ]:
T1 = 1,
T2 = T1
print(T2)
T2 += (2, 3)
T2 = T2 + (4, 5)
print(T2)
print(T1)
In [ ]:
T = (0, 2, 4, 6)
print(T[1])
print(T[-2])
print()
print(T[2:])
print(T[-2:])
print()
print(T[: 3])
print(T[1: -1])
print(T[3: 3])
print()
print(T[: : 2])
print(T[: 2: 2])
print(T[: : -2])
print(T[: 1: -2])
print(T[2: : -2])
print(T[4: 0: -2])
In [ ]:
T = ([[1, 2, 3, 4], [5], [6, 7], [8, 9, 10]],
[[11, 12], [13, 14, 15, 16, 18], [19, 20]],
[[21], [22], [23, 24, 25], [26, 27], [28, 29, 30]])
print(T[1])
print(T[1][1])
print(T[1][1][3])
print()
T1 = T
T[1][1][3] = [0]
print(T)
T[1][1] = 0
print(T)
print(T1 == T)