+ 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?

23rd Jun 2018, 8:30 PM
Muhammad Hasan
Muhammad Hasan - avatar
2 ответов
+ 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()}
24th Jun 2018, 8:11 AM
Cépagrave
Cépagrave - avatar
+ 3
Thank You!!!
24th Jun 2018, 8:09 AM
Muhammad Hasan
Muhammad Hasan - avatar