0

I wrote this code as a test to implement it later in the script I'm working on. The concept is simple, sorting a dictionary in different ways based on the user's choice.

"a" is the default order (the order of data entry in the dictionary). "b" is the default order but reversed. "c" is the default order, but in alphabetical order (A-Z). "d" is the order of "c" but reversed (Z-A).

a = {"pfdee": 100, "pafeeg": 1000, "ddsds": 999, "gfgfgh": 10}
b = {i: a[i] for i in reversed(list(a.keys()))}
c = {i: a[i] for i in sorted(list(a.keys()))}
d = {i: a[i] for i in sorted(list(a.keys()), reverse=True)}

print("a: " + str(a))
print("b: " + str(b))
print("c: " + str(c))
print("d: " + str(d))

Now, I would like to add two further orders, that is related to the quantity of each single object (so "e" should be from the smallest to the largest, "f" should be from the largest to the smallest). I did several tests, but unfortunately they were unsuccessful. I could do it in a normal for loop, but I'd like to keep the dictionary comprehensions if possible. So I ask for advice on how I should proceed, to have a better understanding.

I specify that I am using Python 3.7

BlackFenix06
  • 577
  • 1
  • 6
  • 22

3 Answers3

2

You can sort a.items() (key-value pairs) and use the values as the sorting criteria:

a = {"pfdee": 100, "pafeeg": 1000, "ddsds": 999, "gfgfgh": 10}

e = {k : v for k, v in sorted(a.items(), key = lambda t : t[1])}
f = {k : v for k, v in sorted(a.items(), key = lambda t : t[1], reverse = True)}

print("e: " + str(e))
print("f: " + str(f))

Output:

e: {'gfgfgh': 10, 'pfdee': 100, 'ddsds': 999, 'pafeeg': 1000}
f: {'pafeeg': 1000, 'ddsds': 999, 'pfdee': 100, 'gfgfgh': 10}

In this case, since you have a list of key-value tuples, you can just use the dict constructor instead of a dict comprehension:

e = dict(sorted(a.items(), key = lambda t : t[1]))
f = dict(sorted(a.items(), key = lambda t : t[1], reverse = True))
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
1

sorted takes an optional argument, called key, so you can specify a function, which returns an integer, by which sorting of the elements will be done.

e = {i: a[i] for i in sorted(list(a.keys(), key = lambda e: len(e))}
f = {i: a[i] for i in sorted(list(a.keys(), key = lambda e: -len(e))}

Though i have to warn you your code makes no sense, as dictionary is a sequence of unordered keys mapping their values, so all of those dictionaries are the same. Try looking at the module collections and OrderedDict structure it contains.

kuco 23
  • 786
  • 5
  • 18
  • 1
    Thank you for your answer. I stated in my question that I use Python 3.7, just because from Python 3.6+ the dictionaries are sorted. – BlackFenix06 Sep 19 '19 at 20:21
1

If I don't understand incorrectly this might help you get what you need. Are you talking about smallest to largest as in the keys? Or smallest to largest for the values of each key? My answer adresses the first case. Let me know please so I can delete it, since the other situation has already been answered.

a = {"pfdee": 100, "pafeeg": 1000, "ddsds": 999, "gfgfgh": 10}
b = {i: a[i] for i in reversed(list(a.keys()))}
c = {i: a[i] for i in sorted(list(a.keys()))}
d = {i: a[i] for i in sorted(list(a.keys()), reverse=True)}
e = {i: a[i] for i in sorted(a.keys(),key=len)}
f = {i: a[i] for i in sorted(a.keys(),key=len,reverse=True)}
print("a: " + str(a))
print("b: " + str(b))
print("c: " + str(c))
print("d: " + str(d))
print("e: " + str(e))
Celius Stingher
  • 17,835
  • 6
  • 23
  • 53