+ 1

format and dict question

d = {10:"x", 1:"wx", 2:"yz"} a = d.setdefault(1) b = d.setdefault(3) s = "{}" * len(d) print(s.format(*d)) Why will the output always be 11032?

5th Mar 2018, 8:13 AM
leobing001
4 Respostas
+ 2
because you are printing the keys inside d row 2 moves key 1 to the front row 3 tries to call for key 3 which doesn't exists so it adds 3: None in so dict will be {1:"wx", 10:"x", 3:None, 2:"yz"} keys printed 11032 what are you trying to accomplish, want help?
5th Mar 2018, 8:46 AM
Markus Kaleton
Markus Kaleton - avatar
+ 1
thanks very much for answering me, but why does row 2 move key 1 to the front? and why dose key 3 insert between 10 and 2?
5th Mar 2018, 1:56 PM
leobing001
0
i dont know to say whitout testing but i also think that the way you are formatting that thing suffles it around abit
5th Mar 2018, 2:11 PM
Markus Kaleton
Markus Kaleton - avatar
0
depending on what you are trying to accomplish you might want to change the code a bit
5th Mar 2018, 2:14 PM
Markus Kaleton
Markus Kaleton - avatar