11

When I introduce new pair it is inserted at the beginning of dictionary. Is it possible to append it at the end?

iOS
  • 813
  • 5
  • 14
user1801180
  • 125
  • 1
  • 1
  • 5

5 Answers5

19

UPDATE

As of Python 3.7, dictionaries remember the insertion order. By simply adding a new value, you can be sure that it will be "at the end" if you iterate over the dictionary.


Dictionaries have no order, and thus have no beginning or end. The display order is arbitrary. If you need order, you can use a list of tuples instead of a dict:

In [1]: mylist = []

In [2]: mylist.append(('key', 'value'))

In [3]: mylist.insert(0, ('foo', 'bar'))

You'll be able to easily convert it into a dict later:

In [4]: dict(mylist)
Out[4]: {'foo': 'bar', 'key': 'value'}

Alternatively, use a collections.OrderedDict as suggested by IamAlexAlright.

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
7

A dict in Python is not "ordered" - in Python 2.7+ there's collections.OrderedDict, but apart from that - no... The key point of a dictionary in Python is efficient key->lookup value... The order you're seeing them in is completely arbitrary depending on the hash algorithm...

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • It depends on the hash algorithm and also the order the items are inserted (when collisions happen). – mgilson Nov 05 '12 at 20:07
  • @mgilson Yup - completely different from a C++ `map` - which ensures ordering of keys - etc.. etc... – Jon Clements Nov 05 '12 at 20:09
  • 1
    I wouldn't know about C++ ... My first programming class was C++ and I *hated* it. I thought programming was the stupidest thing ever. Ironically, eventually I grew to like programming when I started programming in C and then Fortran after that (seems like I went backwards quite a bit). And python, Fortan and C are pretty much the only languages that I code in now -- with a little shell scripting here and there when necessary. – mgilson Nov 05 '12 at 20:15
  • @mgilson I'm not sure there's a "good" language to learn with... all have their own strengths/weaknesses and it's only after time you "get it" (or at least you *think* you get it)... I'm quite lucky 'cos I think I'm competent with Python, but also have others under my belt, so who knows... It feels painful to program C again though ;) – Jon Clements Nov 05 '12 at 20:20
  • Yeah, I think the problem was that they were trying to teach me to program instead of trying to teach me to do something interesting using programming. Once I had a solid reason to write programs to solve interesting problems, it became a lot more fun. And you can imagine that when I get to program in C, it's like driving a Cadillac compared to writing Fortran code (*I can group a bunch of arguments together and call it a "struct"? Cool!*). Of course, that makes Python a Ferrari ( In terms of coding experience at least -- Not exactly in terms of performance of the resulting program :-D ) – mgilson Nov 05 '12 at 20:24
5

No. Check the OrderedDict from collections module.

alexvassel
  • 10,600
  • 2
  • 29
  • 31
0

dictionary data is inorder collection if u add data to dict use this : Adding a new key value pair

Dic.update( {'key' : 'value' } )

If key is string you can directly add without curly braces

Dic.update( key= 'value' )
mamal
  • 1,791
  • 20
  • 14
0

If you intend for updated values to move to the end of the dict then you can pop the key first then update the dict.

For example:

In [1]: number_dict = {str(index): index for index in range(10)}
In [2]: number_dict.update({"3": 13})
In [3]: number_dict
Out[3]: 
{'0': 0,
 '1': 1,
 '2': 2,
 '3': 13,
 '4': 4,
 '5': 5,
 '6': 6,
 '7': 7,
 '8': 8,
 '9': 9}

In [4]: number_dict = {str(index): index for index in range(10)}
In [5]: number_dict.pop("3", None)
In [6]: number_dict.update({"3": 13})
In [7]: number_dict
Out[7]: 
{'0': 0,
 '1': 1,
 '2': 2,
 '4': 4,
 '5': 5,
 '6': 6,
 '7': 7,
 '8': 8,
 '9': 9,
 '3': 13}
SixenseMan
  • 145
  • 2
  • 4