1

I have

x = {2:[40,30],3:[24,16],4:[30,2],5:[0,80],6:[19,8]}

and I need to sort dictionary based on the 1st item in the value so my result should be

{2:[40,30],4:[30,2],3:[24,16],6:[19,8],5:[0,80]}
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
FryMan
  • 51
  • 2
  • 9
  • 1
    Possible duplicate of [Python: List to Dictionary](http://stackoverflow.com/questions/4576115/python-list-to-dictionary) – konkked Mar 04 '16 at 03:44

3 Answers3

3

All you need to do is using dict

d = dict(x)

Since your list is already in the right format to be easily converted to Dictionary, that is, you already have a key-value tuple pair as your list elements.

Ian
  • 30,182
  • 19
  • 69
  • 107
  • but if i have x = [(4, [30, 2]), (2, [40, 30])] on calling dict(x) i'll get {2: [40, 30], 4: [30, 2]} but i want answer in such a way that order should not be changed, i should get { 4: [30, 2], 2: [40, 30]} – FryMan Mar 04 '16 at 03:41
  • 2
    @SagarR unfortunately, `Dictionary` does not have concept of `Order`. `Order` is concept in `list`. Dictionary runs with `Key-Value` pair concept. You use key and you get the value, that is why it is unordered. Another reason is that it is done this way to gain some performance benefit internally. You need to find the right tool for your use. If you need it to be ordered, please use `List`, don't use `Dict`. if you need key value pair, use `dict`. If you need both, then you may create both - something along that line – Ian Mar 04 '16 at 03:44
  • is this possible if i have x = {2:[40,30],3:[24,16],4:[30,2],5:[0,80],6:[19,8]} and i need to sort dictionary based on the 1st item in the value so my result should be {2:[40,30],4:[30,2],3:[24,16],6:[19,8],5:[0,80]} – FryMan Mar 04 '16 at 03:53
  • 1
    @SagarR to keep it ordered, you *can't* use dictionary. You may want to look int [OrderedDict](http://stackoverflow.com/questions/1867861/python-dictionary-keep-keys-values-in-same-order-as-declared) though. – Ian Mar 04 '16 at 03:56
  • @SagarR you mean the `list`, `dict`, and `OrderedDict`? great! :) find your *right* tool, that is the key... – Ian Mar 04 '16 at 04:15
2

Simply pass it into dict():

>>> dict([(2, [40, 30]), (4, [30, 2])])
{2: [40, 30], 4: [30, 2]}

Dictionaries don't care what order their values are in:

>>> {'one': 1, 'two': 2} == {'two': 2, 'one': 1}
True

If you want to maintain order, you can use collections.OrderedDict:

>>> from collections import OrderedDict
>>> OrderedDict([(2, [40, 30]), (4, [30, 2])])
OrderedDict([(2, [40, 30]), (4, [30, 2])])
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • but if i have x = [(4, [30, 2]), (2, [40, 30])] on calling dict(x) i'll get {2: [40, 30], 4: [30, 2]} but i want answer in such a way that order should not be changed, i should get { 4: [30, 2], 2: [40, 30]} – FryMan Mar 04 '16 at 03:39
  • @SagarR, I have added an example using `collections.OrderedDict`, which cares about the order of its elements (unlike `dict`). – ChrisGPT was on strike Mar 04 '16 at 03:45
  • is this possible if i have x = {2:[40,30],3:[24,16],4:[30,2],5:[0,80],6:[19,8]} and i need to sort dictionary based on the 1st item in the value so my result should be {2:[40,30],4:[30,2],3:[24,16],6:[19,8],5:[0,80]} – FryMan Mar 04 '16 at 03:53
  • @SagarR, you keep changing the rules. First you asked how to create a `dict`, and two people showed you. Then you changed your question to ask how to maintain order, and I updated my answer and you also got a _new_ answer talking about `OrderedDict`. Now you are asking how to _change_ the order based on the value being stored. Please make sure you know what you actually want and include those details in your question. I am leaving my answer as-is since I have answered two of your questions correctly. Feel free to ask a _new_ question with your new rules. – ChrisGPT was on strike Mar 04 '16 at 03:56
  • first i had x = {2:[40,30],3:[24,16],4:[30,2],5:[0,80],6:[19,8]} then i called sorted(x.items(), key=operator.itemgetter(1), reverse=True) it gave me [(2, [40, 30]), (4, [30, 2]), (3, [24, 16]), (6, [19, 8]), (5, [0, 80])] so i asked a question to convert to dict in a same order... – FryMan Mar 04 '16 at 04:00
  • @SagarR, so put that into `collections.OrderedDict`. – ChrisGPT was on strike Mar 04 '16 at 04:07
1

You weren't explicit in your question, but from your comments on other answers you want to preserve the order of the list in dictionary form. A standard dictionary is inherently unordered, however, you can use an OrderedDict instead:

from collections import OrderedDict

x =  [(4, [30, 2]), (2, [40, 30])]
d = dict(x)
print(d)
# {2: [40, 30], 4: [30, 2]}

d = OrderedDict(x)
print(d)
# OrderedDict([(4, [30, 2]), (2, [40, 30])])

This preserves the keys in order of addition to the dictionary.

mhawke
  • 84,695
  • 9
  • 117
  • 138