1>> d = {'a': 'Arthur', 'b': 'Belling'}
2
3>> d.items()
4[('a', 'Arthur'), ('b', 'Belling')]
5
6>> d.keys()
7['a', 'b']
8
9>> d.values()
10['Arthur', 'Belling']
11
1my_list = ['Nagendra','Babu','Nitesh','Sathya']
2my_dict = dict()
3for index,value in enumerate(my_list):
4 my_dict[index] = value
5print(my_dict)
6#OUTPUT
7{0: 'Nagendra', 1: 'Babu', 2: 'Nitesh', 3: 'Sathya'}
1# This is our example dictionary
2petAges = {"Cat": 4, "Dog": 2, "Fish": 1, "Parrot": 5}
3# This will be our list, epmty for now
4petAgesList = []
5# Search through the dictionary and find all the keys and values
6for key, value in petAges.items():
7 petAgesList.append([key, value]) # Add the key and value to the list
8print(petAgesList) # Print the now-filled list
9# Output: [['Cat', 4], ['Dog', 2], ['Fish', 1], ['Parrot', 5]]
1# Python3 program to Convert a
2# list to dictionary
3
4def Convert(lst):
5 res_dct = {lst[i]: lst[i + 1] for i in range(0, len(lst), 2)}
6 return res_dct
7
8# Driver code
9lst = ['a', 1, 'b', 2, 'c', 3]
10print(Convert(lst))
1#python convert a dict to list or a list to dict or a slice a dict or sort a dict by key or value without import
2#convert dict to list or list to dict or slice a dict
3#1. convert dict to list
4temp = [] # not required as [] is mentioned below
5s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}#our dict remember
6temp = [[k,v]for k,v in s.items()]# temp is now list
7print(temp)#[['b', 3], ['a', 2], ['c', 2], ['d', 1], ['e', 1]]
8
9#2. list to dict || pass a list like below
10a_dict ={}# not required since we have {} in below
11a_dict = { v[0]:v[1] for k,v in enumerate(temp)}
12# OR || a_dict = { v[0]:v[1] for k,v in enumerate([k,v]for k,v in s.items())}
13print(a_dict)#{'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}
14
15#3. Slice a dict is as simple slice as slicing a list
16#see below at temp[0:3] to slice dict to first 4 elements (0 to 3)
17a_dict = { v[0]:v[1] for k,v in enumerate(temp[0:3])}
18print(a_dict)#{'b': 3, 'a': 2, 'c': 2}
19# OR Also like doing this ([[k,v]for k,v in s.items()][0:3])
20#a_dict = { v[0]:v[1] for k,v in enumerate([[k,v]for k,v in s.items()][0:3])}
21print(a_dict)#{'b': 3, 'a': 2, 'c': 2}
22
23#Sort a dict #Reverse True for desc and False for asc
24#4. Sort a dict by key || s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}
25a_dict = dict(sorted(s.items(),key=lambda x:x[0],reverse = False))
26print(a_dict )# {'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
27#Sort a dict by value || put 1 in x:x[1]
28a_dict = dict(sorted(s.items(),key=lambda x:x[1],reverse = False))
29#{'d': 1, 'e': 1, 'a': 2, 'c': 2, 'b': 3}
30
31#5. Sort dict with import operator
32import operator as op#change itemgetter to 0|1 for key|value
33a_dict = dict(sorted(s.items(),key=op.itemgetter(0), reverse = False))
34print(a_dict)#{'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
35#refer to #python convert a dict to list or a list to dict or a slice a dict or sort a dict by key or value without import
36#convert dict to list or list to dict or slice a dict
37#1. convert dict to list
38temp = [] # not required as [] is mentioned below
39s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}#our dict remember
40temp = [[k,v]for k,v in s.items()]# temp is now list
41print(temp)#[['b', 3], ['a', 2], ['c', 2], ['d', 1], ['e', 1]]
42
43#2. list to dict || pass a list like below
44a_dict ={}# not required since we have {} in below
45a_dict = { v[0]:v[1] for k,v in enumerate(temp)}
46# OR || a_dict = { v[0]:v[1] for k,v in enumerate([k,v]for k,v in s.items())}
47print(a_dict)#{'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}
48
49#3. Slice a dict is as simple slice as slicing a list
50#see below at temp[0:3] to slice dict to first 4 elements (0 to 3)
51a_dict = { v[0]:v[1] for k,v in enumerate(temp[0:3])}
52print(a_dict)#{'b': 3, 'a': 2, 'c': 2}
53# OR Also like doing this ([[k,v]for k,v in s.items()][0:3])
54#a_dict = { v[0]:v[1] for k,v in enumerate([[k,v]for k,v in s.items()][0:3])}
55print(a_dict)#{'b': 3, 'a': 2, 'c': 2}
56
57#Sort a dict #Reverse True for desc and False for asc
58#4. Sort a dict by key || s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}
59a_dict = dict(sorted(s.items(),key=lambda x:x[0],reverse = False))
60print(a_dict )# {'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
61#Sort a dict by value || put 1 in x:x[1]
62a_dict = dict(sorted(s.items(),key=lambda x:x[1],reverse = False))
63#{'d': 1, 'e': 1, 'a': 2, 'c': 2, 'b': 3}
64
65#5. Sort dict with import operator
66import operator as op#change itemgetter to 0|1 for key|value
67a_dict = dict(sorted(s.items(),key=op.itemgetter(0), reverse = False))
68print(a_dict)#{'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
69#refer to https://ideone.com/uufYuP
70#dn't forget to upvote