0

Why the output here is like this

numbers = ["one","two","three","four"] num_dict = {x[0]:x for x in numbers} print(num_dict['t']) the output is three but I have no idea why is that

7th Jan 2020, 8:47 PM
THEGreatGatsby
THEGreatGatsby - avatar
1 Answer
0
In the for loop you create a dictionary wich would look like this: num_dict = {'o':'one', 't':'two','t':'three','f':'four'}, but now you have the key 't' twice. Because every key needs to be unique python will just assign a new value to the key 't' wich mean 't':'two' -> 't':'three'. If you call the key 't' it will print the current value which is 'three'.
17th Apr 2020, 7:32 PM
Felix Stolle
Felix Stolle - avatar