+ 1

# For some reason this look alien to me. Can someone explain this?

tuple = {} tuple[(1, 2, 3)] = 8 tuple[(4, 2, 1)] = 10 tuple[(1, 2)] = 12 _sum = 0 for k in tuple: _sum += tuple[k] print(len(tuple) + _sum)

1st Feb 2022, 9:20 PM
cryptic blake + 3
Hello cryptic blake You have a dictionary called tuple (btw a bad name) The keys of your dictionary are tuples and 8, 10 and 12 are the values. If you print your dictionary: {(1,2,3):8 , (4,2,1):10, (1,2):12} Now you loop through the keys and add each value of this key to _sum k is (1,2,3) -> add 8 to sum k is (4,2,1) -> add 10 to sum k is (1,2) -> add 12 to sum Sum is 30 + length of your dictionary which is 3. So you get 33 as output.
1st Feb 2022, 9:42 PM
Denise Roßberg + 3
Hi! I don’t know that you trying to do, but it it is strange to call a dictionary a tuple?! You can never assign a value to a created tuple, because it’s imutable. So what you are doing is a assigning values to a dictionary, called ’tuple’, using a tuples as keys. If you use ’tuple’ as a variable name you have to delete before you can use the inbuilt function tuple(). Better find another name like mydict = {} In the for loop you scan over the dictionaries keys, return there values and sum them up in _sum. But you are missing a parentheses in the print function. The result will be the sum of the values plus the length of the dictionary (the number of keys in the dictionary): 3 + (8+10+12) = 33).
1st Feb 2022, 9:27 PM
Per Bratthammar + 2
I guess what I didn't know is that a dictionary can have a tuple as key. This is a question I got from a Python challenge This learning process can be confusing sometimes lol. Simple stuff can seem difficult when treated with difficulty🥴 Anyways thank you all for your invaluable help!
1st Feb 2022, 10:06 PM
cryptic blake + 2
cryptic blake In short: keys must be immutable and tuples are immutable. So you can use tuples as keys. https://www.askpython.com/JUMP_LINK__&&__python__&&__JUMP_LINK/dictionary/python-dictionary-dict-tutorial
2nd Feb 2022, 3:18 AM
Denise Roßberg 