# example
= tuple(["foo", [1,2], True])
tup
2] = False tup[
TypeError: 'tuple' object does not support item assignment
Python basics
Kunal Khurana
February 16, 2024
TypeError: 'tuple' object does not support item assignment
a = 1, b = 2, c= 3
a = 4, b = 5, c= 6
a = 7, b = 8, c= 9
[4, 5, 6, None, 'foo', 7, 8, (1, 2)]
[4, 5, 6, None, 'foo', 7, 8, (1, 2)]
[4, 5, 6, None, 'foo', 7, 8, (1, 2)]
[4, 5, 6, None, 'foo', 7, 8, (1, 2)]
[4, 5, 6, None, 'foo', 7, 8, (1, 2)]
[4, 5, 6, None, 'foo', 7, 8, (1, 2)]
[4, 5, 6, None, 'foo', 7, 8, (1, 2)]
[4, 5, 6, None, 'foo', 7, 8, (1, 2)]
[4, 5, 6, None, 'foo', 7, 8, (1, 2)]
[4, 5, 6, None, 'foo', 7, 8, (1, 2)]
[4, 5, 6, None, 'foo', 7, 8, (1, 2)]
[4, 5, 6, None, 'foo', 7, 8, (1, 2)]
[4, 5, 6, None, 'foo', 7, 8, (1, 2)]
[4, 5, 6, None, 'foo', 7, 8, (1, 2)]
[4, 5, 6, None, 'foo', 7, 8, (1, 2)]
[4, 5, 6, None, 'foo', 7, 8, (1, 2)]
{4: 1, 5: 2, 6: 3, None: 4}
# write a function to club the words by same first alphabet
words = ['apple', 'bat', 'bar', 'atom', 'book'] # list
by_letter = {} #empty dict
for word in words:
letter = word[0] #first goes in
if letter not in by_letter:
by_letter[letter] = [word]
else:
by_letter[letter].append(word)
print(by_letter)
{'a': ['apple', 'atom'], 'b': ['bat', 'bar', 'book']}