+ 9
Changing dictionaries into some list? and swapping key with value
Suppose I have diction = {1:"a",2:"b",3:"c",4:"d"} how do i change it to list = [(1,"a"),(2,"b"),(3,"c"),(4,"d")] and how can I swap the diction key and value? so I'll have diction = {"a":1,"b":2,"c":3,"d":4} Any idea?
2 Answers
+ 20
dic = {1:"a",2:"b",3:"c",4:"d"}
lst = [(i,j)for i,j in dic.items()]
dic = {j:i for i,j in dic.items()}
+ 3
Thank You!!!