python slice a dict

Solutions on MaxInterview for python slice a dict by the best coders in the world

showing results for - "python slice a dict"
María Fernanda
10 Jun 2018
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