1

I always get my dictionary disordering the data , what I'm doing wrong here ?

data={
'1': '1',
'2': '2',
'3': '3',
'4': '4',
'5': '5',
'6': '6',
'7': '7',
'8': '8',
'9': '9',
'10': '10',
'11': '11'
}
print data

Result:

{'11': '11', '10': '10', '1': '1', '3': '3', '2': '2', '5': '5', '4': '4', '7':'7', '6': '6', '9': '9', '8': '8'}

What do I have to do to get it in the right order?

PS : that list was just an example my list type is more complicated : data={ 'str1': 'str2', 'str3': 'str4', 'str5': 'str6' ....} how can i keep them in order like i write them in the first place ? with the key:value format

  • 1
    I recommend to read the [Python tutorial](https://docs.python.org/2/tutorial/datastructures.html#dictionaries) (emphasis mine): *"It is best to think of a dictionary as an **unordered set** of key: value pairs, "* – Felix Kling Sep 19 '15 at 20:50
  • Where did you get the idea that Python dictionaries are ordered? Did you try running a Google search with "Python" plus your question's title? – TigerhawkT3 Sep 19 '15 at 21:12
  • And why does this useless zero-research question have three upvotes... – TigerhawkT3 Sep 19 '15 at 21:18

4 Answers4

7

Dictionaries in Python don't know anything about order. You can use OrderedDict instead!

cdonts
  • 9,304
  • 4
  • 46
  • 72
0

Use OrderedDict to keep the items in the same order that you wrote them in the source file (or inserted them):

from collections import OrderedDict

data = OrderedDict([
    ('1', '1'),
    ('2', '2'),
    ('3', '3'),
    ('4', '4'),
    ('5', '5'),
    ('6', '6'),
    ('7', '7'),
    ('8', '8'),
    ('9', '9'),
    ('10', '10'),
    ('11', '11'),
  ])
print data

Output:

OrderedDict([('1', '1'), ('2', '2'), ('3', '3'), ('4', '4'), ('5', '5'), ('6', '6'), ('7', '7'), ('8', '8'), ('9', '9'), ('10', '10'), ('11', '11')])

Although the output doesn't look like a normal dict in this case, the "data" variable will still behave like a normal dict when you iterate over it or index into it.

dbort
  • 962
  • 8
  • 15
0

first you can represent your data in the following way, for the program recognize the characters between '' as the keys and the others as the values:

data={
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'10': 10,
'11': 11
}

Now you could apply the example in the book pythonlearn page 122 to show your data ordered by value. It is used a simple for loop to append the dictionary items to a list and then sorted them by value.

l = list()
for key, val in data.items() :
    l.append( (val, key) )
l.sort()
print l
for key, val in l:
    print key, val
[(1, '1'), (2, '2'), (3, '3'), (4, '4'), (5, '5'), (6, '6'), (7, '7'), (8, '8'), (9, '9'), (10, '10'), (11, '11')]
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
TatiAuza
  • 11
  • 2
  • you can omit any of the **print** according to the way you wnaat to show the result. – TatiAuza Sep 19 '15 at 22:07
  • and how i order key:value ? keep getting the error : ` l.append( (val ':' key) ) ^ SyntaxError: invalid syntax` – FreeSteampowresGames Sep 20 '15 at 07:34
  • It could be the Python version, I work with 3.3 so maybe you just need to change the 'l.append( (val, key) )' for l.append(val, key), look that is a Syntax error. You can write just one of the **print** lines according to the way you need your data to show up. – TatiAuza Sep 21 '15 at 14:36
-1
    d={
    '1': '1',
    '2': '2',
    '3': '3',
    '4': '4',
    '5': '5',
    '6': '6',
    '7': '7',
    '8': '8',
    '9': '9',
    '10': '10',
    '11': '11'
    }


    from collections import OrderedDict

    od = OrderedDict(sorted(d.items()))

    print(od)

OrderedDict([('1', '1'), ('10', '10'), ('11', '11'), ('2', '2'), ('3', '3'), ('4', '4'), ('5', '5'), ('6', '6'), ('7', '7'), ('8', '8'), ('9', '9')])

LetzerWille
  • 5,355
  • 4
  • 23
  • 26
  • 1
    sorted order is not the same as insertion order, an OrderedDict keeps the keys in the order you add then not in sorted order – Padraic Cunningham Sep 19 '15 at 21:02
  • person asking the question wanted to preserve the order that was in his data. That data was sorted that is why i have used sort before inserting data into Orderded dict. – LetzerWille Sep 19 '15 at 21:17
  • If you look at the comment to the answer about you will see that sorting is not what the OP wants, on another note your order is wrong as your are sorting *lexicographically* i.e `"2" > "10"` – Padraic Cunningham Sep 19 '15 at 21:20