fork download
  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
  4. temp = [] # not required as [] is mentioned below
  5. s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}#our dict remember
  6. temp = [[k,v]for k,v in s.items()]# temp is now list
  7. print(temp)#[['b', 3], ['a', 2], ['c', 2], ['d', 1], ['e', 1]]
  8.  
  9. #2. list to dict || pass a list like below
  10. a_dict ={}# not required since we have {} in below
  11. a_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())}
  13. print(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)
  17. a_dict = { v[0]:v[1] for k,v in enumerate(temp[0:3])}
  18. print(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])}
  21. print(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}
  25. a_dict = dict(sorted(s.items(),key=lambda x:x[0],reverse = False))
  26. print(a_dict )# {'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
  27. #Sort a dict by value || put 1 in x:x[1]
  28. a_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
  32. import operator as op#change itemgetter to 0|1 for key|value
  33. a_dict = dict(sorted(s.items(),key=op.itemgetter(0), reverse = False))
  34. print(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
  38. temp = [] # not required as [] is mentioned below
  39. s = {'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}#our dict remember
  40. temp = [[k,v]for k,v in s.items()]# temp is now list
  41. print(temp)#[['b', 3], ['a', 2], ['c', 2], ['d', 1], ['e', 1]]
  42.  
  43. #2. list to dict || pass a list like below
  44. a_dict ={}# not required since we have {} in below
  45. a_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())}
  47. print(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)
  51. a_dict = { v[0]:v[1] for k,v in enumerate(temp[0:3])}
  52. print(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])}
  55. print(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}
  59. a_dict = dict(sorted(s.items(),key=lambda x:x[0],reverse = False))
  60. print(a_dict )# {'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
  61. #Sort a dict by value || put 1 in x:x[1]
  62. a_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
  66. import operator as op#change itemgetter to 0|1 for key|value
  67. a_dict = dict(sorted(s.items(),key=op.itemgetter(0), reverse = False))
  68. print(a_dict)#{'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
  69. #refer to https://i...content-available-to-author-only...e.com/uufYuP
  70. #dn't forget to upvote
Success #stdin #stdout 0.02s 9048KB
stdin
Standard input is empty
stdout
[['b', 3], ['a', 2], ['c', 2], ['d', 1], ['e', 1]]
{'b': 3, 'a': 2, 'c': 2, 'd': 1, 'e': 1}
{'b': 3, 'a': 2, 'c': 2}
{'b': 3, 'a': 2, 'c': 2}
{'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}
{'a': 2, 'b': 3, 'c': 2, 'd': 1, 'e': 1}