0
dict_d = {'Blue': [('Camaro', 10, 12)], 'Red': [('Camara', 10, 5)], 'Green': [('Nancy', 10, 8), ('Steve', 7, 8), ('Rich', 10, 4)]}

I have a dictionary above, and I only want to sort the one with multiple values, which is "Green" in this case. I want to sort it by name. Basically, what I want to get is:

{'Blue': [('Camaro', 10, 12)], 'Red': [('Camara', 10, 5)], 'Green': [('Nancy', 10, 8), ('Rich', 10, 4), ('Steve', 7, 8)]}

The difference is the that the tuples in Green has been sorted by name.

Here is what I have tried:

sorted((dict_d).items(), key = (lambda x: list(x)[1][0]))

It did not give me any error and it did not sort..... So confused...

AChampion
  • 29,683
  • 4
  • 59
  • 75
  • Welcome to Stack Overflow! You seem to be asking for someone to write some code for you. Stack Overflow is a question and answer site, not a code-writing service. Please [see here](http://stackoverflow.com/help/how-to-ask) to learn how to write effective questions. – zondo Apr 07 '16 at 02:13
  • Dictionaries are _unordered_. They _cannot_ be sorted, you can only get a string representation that _appears_ sorted. Use an OrderedDict instead! – Akshat Mahajan Apr 07 '16 at 02:21
  • 1
    @AkshatMahajan - `sorted()` produces a `list`, not a string. Also, OP's code may be sorting a dictionary, but what they actually want to do is sort the values contained in the dictionary. – TigerhawkT3 Apr 07 '16 at 02:23
  • Oh. Read it too quickly. Sorry. Thanks for pointing it out! @TigerhawkT3: Why did you reference the behaviour of `sorted()`? I wasn't thinking about it in this case. – Akshat Mahajan Apr 07 '16 at 02:26
  • I'm learning how to use sorted() haha – Frog_Aurora Apr 07 '16 at 02:38
  • Yes. I'm not sorting the dictionary. – Frog_Aurora Apr 07 '16 at 02:38
  • Not sure why this is marked a duplicate of sorting a dictionary by value - the OP is not looking to sort the dictionary at all - but sort the individual values in that dictionary - the answers are very different. – AChampion Apr 11 '16 at 15:35

2 Answers2

1

You are not trying to sort the dictionary but sort the values of the dictionary, you can use a dict comprehension to reconstruct the dictionary with sorted values.

As you are trying to sort by name, just index the name [0] as your sort key.

>>> {k: sorted(v, key=lambda x: x[0]) for k, v in dict_d.items()}
{'Blue': [('Camaro', 10, 12)],
 'Green': [('Nancy', 10, 8), ('Rich', 10, 4), ('Steve', 7, 8)],
 'Red': [('Camara', 10, 5)]}

NB: If you don't care about the order based on the values then you can ignore the key and just sort tuples - sorted(v)

AChampion
  • 29,683
  • 4
  • 59
  • 75
1

Your question has the answers, if you read it carefully:

  1. Loop over the items in your dictionary
  2. If the value (in this case, the list) has length > 1
    • sort the values

Note: When you sort a list, Python's natural method of sorting is to go over each item. Python sorts tuples (in the list) by sort evaluating each item in the tuple. So it compares the first item in each tuple, then the second, and then the third. So the 'names' in those tuples get auto-compared without needing any special comparison function.

>>> d = {'Blue': [('Camaro', 10, 12)], 'Red': [('Camara', 10, 5)], 'Green': [('Nancy', 10, 8), ('Steve', 7, 8), ('Rich', 10, 4)]}
>>> for k, v in d.items():
...     if len(v) > 1:
...         d[k] = sorted(v)
...
>>> d
{'Blue': [('Camaro', 10, 12)], 'Green': [('Nancy', 10, 8), ('Rich', 10, 4), ('Steve', 7, 8)], 'Red': [('Camara', 10, 5)]}

And the if len(v) > 1 step doesn't actually need to be done when your list has only one tuple. Python would have nothing to sort since the list has just one item.

Edit based on what you tried: sorted((dict_d).items(), key = (lambda x: list(x)[1][0])): Construct a new dictionary made up of the sorted values, as per AChampion's answer below.

Community
  • 1
  • 1
aneroid
  • 12,983
  • 3
  • 36
  • 66