#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
#convert dict to list or list to dict or slice a dict
#1. convert dict to list
temp = [] # not required as [] is mentioned below
s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}#our dict remember
temp = [[k,v]for k,v in s.items()]# temp is now list
print(temp)#[['b', 3], ['a', 2], ['c', 2], ['d', 1], ['e', 1]]
#2. list to dict || pass a list like below
a_dict ={}# not required since we have {} in below
a_dict = { v[0]:v[1] for k,v in enumerate(temp)}
# OR || a_dict = { v[0]:v[1] for k,v in enumerate([k,v]for k,v in s.items())}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}
#3. Slice a dict is as simple slice as slicing a list
#see below at temp[0:3] to slice dict to first 4 elements (0 to 3)
a_dict = { v[0]:v[1] for k,v in enumerate(temp[0:3])}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2}
# OR Also like doing this ([[k,v]for k,v in s.items()][0:3])
#a_dict = { v[0]:v[1] for k,v in enumerate([[k,v]for k,v in s.items()][0:3])}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2}
#Sort a dict #Reverse True for desc and False for asc
#4. Sort a dict by key || s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}
a_dict = dict(sorted(s.items(),key=lambda x:x[0],reverse = False))
print(a_dict )# {'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
#Sort a dict by value || put 1 in x:x[1]
a_dict = dict(sorted(s.items(),key=lambda x:x[1],reverse = False))
#{'d': 1, 'e': 1, 'a': 2, 'c': 2, 'b': 3}
#5. Sort dict with import operator
import operator as op#change itemgetter to 0|1 for key|value
a_dict = dict(sorted(s.items(),key=op.itemgetter(0), reverse = False))
print(a_dict)#{'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
#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
#convert dict to list or list to dict or slice a dict
#1. convert dict to list
temp = [] # not required as [] is mentioned below
s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}#our dict remember
temp = [[k,v]for k,v in s.items()]# temp is now list
print(temp)#[['b', 3], ['a', 2], ['c', 2], ['d', 1], ['e', 1]]
#2. list to dict || pass a list like below
a_dict ={}# not required since we have {} in below
a_dict = { v[0]:v[1] for k,v in enumerate(temp)}
# OR || a_dict = { v[0]:v[1] for k,v in enumerate([k,v]for k,v in s.items())}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}
#3. Slice a dict is as simple slice as slicing a list
#see below at temp[0:3] to slice dict to first 4 elements (0 to 3)
a_dict = { v[0]:v[1] for k,v in enumerate(temp[0:3])}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2}
# OR Also like doing this ([[k,v]for k,v in s.items()][0:3])
#a_dict = { v[0]:v[1] for k,v in enumerate([[k,v]for k,v in s.items()][0:3])}
print(a_dict)#{'b': 3, 'a': 2, 'c': 2}
#Sort a dict #Reverse True for desc and False for asc
#4. Sort a dict by key || s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}
a_dict = dict(sorted(s.items(),key=lambda x:x[0],reverse = False))
print(a_dict )# {'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
#Sort a dict by value || put 1 in x:x[1]
a_dict = dict(sorted(s.items(),key=lambda x:x[1],reverse = False))
#{'d': 1, 'e': 1, 'a': 2, 'c': 2, 'b': 3}
#5. Sort dict with import operator
import operator as op#change itemgetter to 0|1 for key|value
a_dict = dict(sorted(s.items(),key=op.itemgetter(0), reverse = False))
print(a_dict)#{'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
#refer to https://i...content-available-to-author-only...e.com/uufYuP
#dn't forget to upvote
I3B5dGhvbiBjb252ZXJ0IGEgZGljdCB0byBsaXN0IG9yIGEgbGlzdCB0byBkaWN0IG9yIGEgc2xpY2UgYSBkaWN0IG9yIHNvcnQgYSBkaWN0IGJ5IGtleSBvciB2YWx1ZSB3aXRob3V0IGltcG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