1

I have a dictionary as the following data structure:

d = {'TRANSFERRED': [2281, 1031, 1775, 867, 1242],
     'CLOSED':      [239, 269, 645, 540, 388], 
     'DEFERRED':    [89, 5, 68, 48, 37],
     'OPEN':        [3, 0, 2, 1, 0],
     'IN PROGRESS': [0, 2, 4, 0, 5],
     'QUEUED':      [0, 0, 0, 0, 0]}

The dictionary contains lists with numeric values and I would like to order them from the lowest to the highest value, something like this:

d = {'TRANSFERRED': [867, 1031, 1242, 1775, 2281],
     'CLOSED':      [239, 269, 388, 540, 645], 
     'DEFERRED':    [5, 37, 48, 68, 89],
     'OPEN':        [0, 0, 1, 2, 3],
     'IN PROGRESS': [0, 0, 2, 4, 5],
     'QUEUED':      [0, 0, 0, 0, 0]}

I am concerned that dictionaries cannot be sorted because they are inherently orderless but other types such as lists and tuples are not orderless, moreover, I have been using the following trick to order dictionaries with lists that contain single items such as:

d2 = {'TRANSFERRED': [-2281],
      'CLOSED':      [239], 
      'DEFERRED':    [489],
      'OPEN':        [34],
      'IN PROGRESS': [0],
      'QUEUED':      [-10]}
sorted(d2.items(), key=lambda x: x[1], reverse=True)

The result gives the following sorted data structure:

[('DEFERRED', [489]), 
 ('CLOSED', [239]), 
 ('OPEN', [34]), 
 ('IN PROGRESS', [0]), 
 ('QUEUED', [-10]), 
 ('TRANSFERRED', [-2281])]

I want to replicate this same result but with a dictionary with lists of multiple numeric values. How can I achieve this? Please, feel free to use the following link repl.it - sort dictionary with lists of multiple items to test your results. Feedback or comments to improve this question are welcome.

2 Answers 2

2

You can use dict.items():

d = {'TRANSFERRED': [2281, 1031, 1775, 867, 1242],
 'CLOSED':      [239, 269, 645, 540, 388], 
 'DEFERRED':    [89, 5, 68, 48, 37],
 'OPEN':        [3, 0, 2, 1, 0],
 'IN PROGRESS': [0, 2, 4, 0, 5],
 'QUEUED':      [0, 0, 0, 0, 0]}
new_d = {a:sorted(b) for a, b in d.items()}

Output:

{'IN PROGRESS': [0, 0, 2, 4, 5], 'TRANSFERRED': [867, 1031, 1242, 1775, 2281], 'DEFERRED': [5, 37, 48, 68, 89], 'CLOSED': [239, 269, 388, 540, 645], 'OPEN': [0, 0, 1, 2, 3], 'QUEUED': [0, 0, 0, 0, 0]}
Sign up to request clarification or add additional context in comments.

1 Comment

Always so fast, just typing out the same solution!
0
d = {
     'TRANSFERRED':2281,
     'CLOSED':239, 
     'DEFERRED':89,
     'OPEN':3,
     'IN PROGRESS':2,
     'QUEUED':0
}

# Some usefull method of dictionary
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#
# To get dict keys:
# ----------------
#
# d.keys() = > dict_keys(['TRANSFERRED', 'CLOSED', 'DEFERRED', 'OPEN', 'IN PROGRESS', 'QUEUED'])
# The output is an iterable.
#
# To get dict values
#-------------------
#
# d.values() => dict_values([2281, 239, 89, 3, 2, 0])
# The out is an iterable.
#
# To get both key and values
# --------------------------
#
# d.items() => dict_items([('TRANSFERRED', 2281), ('CLOSED', 239), ('DEFERRED', 89), ('OPEN', 3),
# ('IN PROGRESS', 2), ('QUEUED', 0)])
# The out is iterable of tuples. Each tuple contains key as the first tuple item and value as
# second item. (k,v)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.