2-List
Sun 29 June 2025
list = [
"AB",
"CD",
"EF",
"GH",
"IJ",
"KL",
"MN",
"OP",
"QR",
"ST",
"UV",
"WX",
"YZ"
]
list
['AB', 'CD', 'EF', 'GH', 'IJ', 'KL', 'MN', 'OP', 'QR', 'ST', 'UV', 'WX', 'YZ']
# append to list
list.append("ABC")
list
['AB',
'CD',
'EF',
'GH',
'IJ',
'KL',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC']
list.extend(["EFG", "IJK", "LMN"])
list
['AB',
'CD',
'EF',
'GH',
'IJ',
'KL',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC',
'EFG',
'IJK',
'LMN']
list.extend(("OPQ", "RST"))
list
['AB',
'CD',
'EF',
'GH',
'IJ',
'KL',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC',
'EFG',
'IJK',
'LMN',
'OPQ',
'RST']
list.extend(range(1, 5))
list
['AB',
'CD',
'EF',
'GH',
'IJ',
'KL',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC',
'EFG',
'IJK',
'LMN',
'OPQ',
'RST',
1,
2,
3,
4]
list2 = ["One", "Two"]
list2
['One', 'Two']
list.append(list2)
list
['AB',
'CD',
'EF',
'GH',
'IJ',
'KL',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC',
'EFG',
'IJK',
'LMN',
'OPQ',
'RST',
1,
2,
3,
4,
['One', 'Two']]
list3 = ["Four", "Five"]
list += list3
list
['AB',
'CD',
'EF',
'GH',
'IJ',
'KL',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC',
'EFG',
'IJK',
'LMN',
'OPQ',
'RST',
1,
2,
3,
4,
['One', 'Two'],
'Four',
'Five']
list.pop(len(list)-1)
'Five'
list
['AB',
'CD',
'EF',
'GH',
'IJ',
'KL',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC',
'EFG',
'IJK',
'LMN',
'OPQ',
'RST',
1,
2,
3,
4,
['One', 'Two'],
'Four']
list.remove('AB')
list
['CD',
'EF',
'GH',
'IJ',
'KL',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC',
'EFG',
'IJK',
'LMN',
'OPQ',
'RST',
1,
2,
3,
4,
['One', 'Two'],
'Four']
list.remove(list2)
list
['CD',
'EF',
'GH',
'IJ',
'KL',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC',
'EFG',
'IJK',
'LMN',
'OPQ',
'RST',
1,
2,
3,
4,
'Four']
list.extend(("ten", "twenty"))
list
['CD',
'EF',
'GH',
'IJ',
'KL',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC',
'EFG',
'IJK',
'LMN',
'OPQ',
'RST',
1,
2,
3,
4,
'Four',
'ten',
'twenty']
list.extend(["eleven", "twelve"])
list
['CD',
'EF',
'GH',
'IJ',
'KL',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC',
'EFG',
'IJK',
'LMN',
'OPQ',
'RST',
1,
2,
3,
4,
'Four',
'ten',
'twenty',
'eleven',
'twelve']
list.append(("six", "seven"))
list
['CD',
'EF',
'GH',
'IJ',
'KL',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC',
'EFG',
'IJK',
'LMN',
'OPQ',
'RST',
1,
2,
3,
4,
'Four',
'ten',
'twenty',
'eleven',
'twelve',
('six', 'seven')]
type(list[len(list)-1])
tuple
del list[len(list)-1]
list
['CD',
'EF',
'GH',
'IJ',
'KL',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC',
'EFG',
'IJK',
'LMN',
'OPQ',
'RST',
1,
2,
3,
4,
'Four',
'ten',
'twenty',
'eleven',
'twelve']
list.remove("ten")
list
['CD',
'EF',
'GH',
'IJ',
'KL',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC',
'EFG',
'IJK',
'LMN',
'OPQ',
'RST',
1,
2,
3,
4,
'Four',
'twenty',
'eleven',
'twelve']
list.append( ["one", "two"])
list
['CD',
'EF',
'GH',
'IJ',
'KL',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC',
'EFG',
'IJK',
'LMN',
'OPQ',
'RST',
1,
2,
3,
4,
'Four',
'twenty',
'eleven',
'twelve',
['one', 'two']]
list.remove(['one', 'two'])
list
['CD',
'EF',
'GH',
'IJ',
'KL',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC',
'EFG',
'IJK',
'LMN',
'OPQ',
'RST',
1,
2,
3,
4,
'Four',
'twenty',
'eleven',
'twelve']
# delete elements 1 - 5
del list[1:5]
list
['CD',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC',
'EFG',
'IJK',
'LMN',
'OPQ',
'RST',
1,
2,
3,
4,
'Four',
'twenty',
'eleven',
'twelve']
list[::2]
['CD', 'OP', 'ST', 'WX', 'ABC', 'IJK', 'OPQ', 1, 3, 'Four', 'eleven']
list
['CD',
'MN',
'OP',
'QR',
'ST',
'UV',
'WX',
'YZ',
'ABC',
'EFG',
'IJK',
'LMN',
'OPQ',
'RST',
1,
2,
3,
4,
'Four',
'twenty',
'eleven',
'twelve']
# Remove every 3rd item
del list[::3]
list
['MN',
'OP',
'ST',
'UV',
'YZ',
'ABC',
'IJK',
'LMN',
'RST',
1,
3,
4,
'twenty',
'eleven']
# Remove every 2nd item
del list[::-3]
list
['MN', 'ST', 'UV', 'ABC', 'IJK', 'RST', 1, 4, 'twenty']
for i, j in enumerate(list):
print(i, j)
0 MN
1 ST
2 UV
3 ABC
4 IJK
5 RST
6 1
7 4
8 twenty
item_index = 1, 4
list = [value for key, value in enumerate(list) if key not in item_index]
list
['MN', 'UV', 'ABC', 'RST', 1, 4, 'twenty']
lst = [3, 1, 4, 1, 5]
print(sorted(lst, reverse=True))
[5, 4, 3, 1, 1]
lst2 = lst[:]
print(lst2 is lst, lst2 == lst)
False True
print(lst[::-1])
[5, 1, 4, 1, 3]
lst3 = [[1,2],[3,4]]
print([x for row in lst3 for x in row])
[1, 2, 3, 4]
print([row[0] for row in lst3])
[1, 3]
print(any([]), all([]))
False True
print(min([5, 2, 9], key=lambda x: -x))
9
print(max([5, 2, 9], key=lambda x: -x))
2
print([(i, val) for i, val in enumerate(["a", "b"])])
[(0, 'a'), (1, 'b')]
print([None]*5)
[None, None, None, None, None]
print([[] for _ in range(3)])
[[], [], []]
print([x+y for x,y in zip([1,2], [3,4])])
[4, 6]
lst = [1,2,3]
lst *= 2
print(lst)
[1, 2, 3, 1, 2, 3]
list = [1, 2, 3]
del list
print(list(reversed([1,2,3]))) # [3,2,1]
[3, 2, 1]
lst = [1,2,3]
lst *= 2
print(lst)
[1, 2, 3, 1, 2, 3]
print(list(map(str.upper, ["a", "b", "c"])))
['A', 'B', 'C']
print(list(filter(str.islower, ["a", "B", "c"])))
['a', 'c']
print([bool(x) for x in [0, 1, "", "text"]])
[False, True, False, True]
lst = [5, 4, 3, 2, 1]
lst.sort()
print(lst)
[1, 2, 3, 4, 5]
lst = [1, 2, 3, 4, 5]
lst[1:4] = [99, 100]
print(lst)
[1, 99, 100, 5]
lst = [1, 2, 3]
lst[1:2] = []
print(lst)
[1, 3]
lst = [1, 2, 3]
lst[:] = [0]
print(lst)
[0]
print([x for x in [1,2,3] if x % 2 == 0])
[2]
print([i for i in range(3, -1, -1)])
[3, 2, 1, 0]
print(list(filter(None, [0, False, 1, 2, None])))
[1, 2]
print([i for i in range(3) if i not in [1]])
[0, 2]
matrix = [[1, 2], [3, 4]]
transposed = list(zip(*matrix))
print(transposed)
[(1, 3), (2, 4)]
print([list(t) for t in zip(*matrix)])
[[1, 3], [2, 4]]
print(all(i < 10 for i in [1, 2, 3]))
True
print(any(i > 3 for i in [1, 2, 4]))
True
print([i for i in range(10) if i not in [2,5,7]])
[0, 1, 3, 4, 6, 8, 9]
print([x for x in [None, 0, "", "Hi"] if x])
['Hi']
print(["even" if i % 2 == 0 else "odd" for i in range(5)])
['even', 'odd', 'even', 'odd', 'even']
print([i*j for i in range(1,4) for j in range(1,4)])
[1, 2, 3, 2, 4, 6, 3, 6, 9]
print([round(i/2,1) for i in range(5)])
[0.0, 0.5, 1.0, 1.5, 2.0]
print([(x, x**2) for x in range(4)])
[(0, 0), (1, 1), (2, 4), (3, 9)]
print([i for i in range(100) if i % 10 == 0])
[0, 10, 20, 30, 40, 50, 60, 70, 80, 90]
print(list(str(i) for i in range(5)))
['0', '1', '2', '3', '4']
lst = [1, 2, 3]
a, b, c = lst
print(a, b, c)
1 2 3
lst = [1, 2, 3]
a, *rest = lst
print(a, rest)
1 [2, 3]
print([i for i in range(10) if i not in (3,6)])
[0, 1, 2, 4, 5, 7, 8, 9]
print([[i for i in range(3)] for _ in range(3)])
[[0, 1, 2], [0, 1, 2], [0, 1, 2]]
print([sum(row) for row in [[1,2],[3,4]]])
[3, 7]
print([row[::-1] for row in [[1,2],[3,4]]])
[[2, 1], [4, 3]]
print(["Yes" if x > 0 else "No" for x in [-1, 0, 1]])
['No', 'No', 'Yes']
print(["FizzBuzz" if i%15==0 else "Fizz" if i%3==0 else "Buzz" if i%5==0 else i for i in range(1,21)])
[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz', 16, 17, 'Fizz', 19, 'Buzz']
print(list(zip("abc", [1,2,3])))
[('a', 1), ('b', 2), ('c', 3)]
print([*range(5)])
[0, 1, 2, 3, 4]
print([chr(i) for i in range(97, 100)])
['a', 'b', 'c']
print([ord(c) for c in "abc"])
[97, 98, 99]
print(["{}:{}".format(i,i**2) for i in range(3)])
['0:0', '1:1', '2:4']
print(list(filter(lambda x: x > 2, [1,2,3,4])))
[3, 4]
print(list(map(lambda x: x**3, [1,2,3])))
[1, 8, 27]
print(list(map(lambda x,y: x+y, [1,2], [3,4])))
[4, 6]
print([i for i in range(3) for _ in range(2)])
[0, 0, 1, 1, 2, 2]
print([i for _ in range(2) for i in range(3)])
[0, 1, 2, 0, 1, 2]
print([x for x in range(5) if x != 2])
[0, 1, 3, 4]
print([[i+j for j in range(3)] for i in range(3)])
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]
print(["Yes" if i%2==0 else "No" for i in range(4)])
['Yes', 'No', 'Yes', 'No']
print(["*"*i for i in range(5)])
['', '*', '**', '***', '****']
print([" ".join([str(j) for j in range(i)]) for i in range(5)])
['', '0', '0 1', '0 1 2', '0 1 2 3']
print([[i]*i for i in range(5)])
[[], [1], [2, 2], [3, 3, 3], [4, 4, 4, 4]]
print(["even"*(i%2==0) or "odd" for i in range(5)])
['even', 'odd', 'even', 'odd', 'even']
print([i for i in range(10) if i & 1 == 0])
[0, 2, 4, 6, 8]
print([i for i in range(10) if bin(i)[-1] == "1"])
[1, 3, 5, 7, 9]
print(list(set([1,2,2,3])))
[1, 2, 3]
print(set([1,2]) & set([2,3]))
{2}
print(set([1,2]) | set([3,4]))
{1, 2, 3, 4}
print(set([1,2]) - set([2]))
{1}
Score: 140
Category: basics