2

I have a dictionary of Arrays in Python such that

dictionary={a:[1,2,3,4],b:[2,3,4,8],c:[3,5,6,5]}

and so on and so on

I've already sorted the dictionary so that it is now sorted by the fourth item of every array so the order goes b,c,a

dictionary=sorted(dictionary.values(),key=itemgetter(3),reverse=True)

The problem is, when I try to iterate through the keys, it shoots out the key as the array itself.

for key in dictionary:
    print(key)

prints out [2,3,4,8], however, I want it to iterate through b,c,a in that order.

Is there an easy way to do this?

nimbyest
  • 193
  • 1
  • 2
  • 11
  • 4
    This is not real code. There isn't a `TRUE` in python. You are searching by `4` but list indexes start from `0` in python so there isn't an element at `4` in your list. The dictionary definition would be invalid because `a`, `b` are undefined. – e4c5 Jun 04 '17 at 05:25
  • What are you trying to do exactly? – syntaxError Jun 04 '17 at 05:27
  • @e4c5 Ah, I apologize, I'm kinda new at stackoverflow and am copying over code from my other computer. As for the dictionary it was just an example, just assume that dictionary['a'] returns back [1,2,3,4] and so forth with b. – nimbyest Jun 04 '17 at 05:35
  • @rosh I'm trying to iterate through the dictionary using the key, but when I sorted the dictionary, the key changed from the normal a,b,c to the array itself. I'm trying to figure out if there is a way that key can still be a,b,c when I use the for loop and not the array. – nimbyest Jun 04 '17 at 05:37
  • It's returning an array because the sorted function returns a list which is now referenced by dictionary. You can't sort a dictionary. – syntaxError Jun 04 '17 at 05:49

2 Answers2

4

You are forgetting a small but very important point about python dictionaries. They cannot be sorted!

What you are actually doing in your code is sorting the dictionary values. To make a dictionary appear to be sorted, you need to created a sorted list which is made up of the dictionary keys.

keys = sorted(dictionary, key=lambda x: (dictionary[x][3],x))
for k in keys:
   print (k, dictionary[k])
e4c5
  • 52,766
  • 11
  • 101
  • 134
  • Oh man this actually worked. Thanks, you're a lifesaver. Though I will have to add a "reverse=True" to the end of the sorted. But thanks, I was spending so long on this problem. – nimbyest Jun 04 '17 at 05:52
  • 1
    Glad to have been of help – e4c5 Jun 04 '17 at 05:53
  • 1
    @nimbyest As with most programming languages, Python sorts smallest-to-largest—the order that we count in. – Arya McCarthy Jun 04 '17 at 07:12
0

Sorted is a builtin function that takes an iterable like a list (array) or dictionary or any other such type and returns a list (array) as output. (Refer here: Builtin Function - sorted()

You cannot sort a dictionary itself and store it as dictionary by itself is unordered (It does not remember the order of its keys).

In order to store a dictionary along with its order use the ordereddict datatype in python. (Refer here for Ordereddict)

For sorting an ordered dict you refer this stackoverflow question.

(For more information on sorting a dictionary Refer this question.)

Kronos
  • 2,210
  • 1
  • 14
  • 10