Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add object to start of dictionary

I am making a group chatting app and I have images associated with the users, so whenever they say something, their image is displayed next to it. I wrote the server in python and the client will be an iOS app. I use a dictionary to store all of the message/image pairs. Whenever my iOS app sends a command to the server (msg:<message), the dictionary adds the image and message to the dictionary like so:dictionary[message] = imageName, which is converted to lists then strings to be sent off in a socket. I would like to add the incoming messages to the start of the dictionary, instead of the end. Something like

#When added to end:
dictionary = {"hello":image3.png}
#new message
dictionary = {"hello":image3.png, "i like py":image1.png}

#When added to start:
dictionary = {"hello":image3.png}
#new message
dictionary = {"i like py":image1.png, "hello":image3.png}

Is there any way to add the object to the start of the dictionary?

like image 465
Minebomber Avatar asked Jun 21 '15 16:06

Minebomber


People also ask

How do you insert an item at the beginning of a dictionary in Python?

To add an item to a Python dictionary, you should assign a value to a new index key in your dictionary. Unlike lists and tuples, there is no add() , insert() , or append() method that you can use to add items to your data structure.

Can you add things to a dictionary in Python?

Python dictionary is one of the built-in data types. Dictionary elements are key-value pairs. You can add to dictionary in Python using multiple methods.


3 Answers

For the use case described it sounds like a list of tuples would be a better data structure.

However, it has been possible to order a dictionary since Python 3.7. Dictionaries are now ordered by insertion order.

To add an element anywhere other than the end of a dictionary, you need to re-create the dictionary and insert the elements in order. This is pretty simple if you want to add an entry to the start of the dictionary.

# Existing data structure
old_dictionary = {"hello": "image3.png"}

# Create a new dictionary with "I like py" at the start, then
# everything from the old data structure.
new_dictionary = {"i like py": "image1.png"}
new_dictionary.update(old_dictionary)

# new_dictionary is now:
# {'i like py': 'image1.png', 'hello': 'image3.png'}
like image 152
Craig Anderson Avatar answered Sep 25 '22 01:09

Craig Anderson


(python3) good example from Manjeet on geeksforgeeks.org

test_dict = {"Gfg" : 5, "is" : 3, "best" : 10}  
updict = {"pre1" : 4, "pre2" : 8}

# ** operator for packing and unpacking items in order
res = {**updict, **test_dict}
like image 30
dev J Avatar answered Sep 26 '22 01:09

dev J


First of all it doesn't added the item at the end of dictionary because dictionaries use hash-table to storing their elements and are unordered. if you want to preserve the order you can use collections.OrderedDict.but it will appends the item at the end of your dictionary. One way is appending that item to the fist of your items then convert it to an Orderd:

>>> from collections import OrderedDict
>>> d=OrderedDict()
>>> for i,j in [(1,'a'),(2,'b')]:
...    d[i]=j
... 
>>> d
OrderedDict([(1, 'a'), (2, 'b')])
>>> d=OrderedDict([(3,'t')]+d.items())
>>> d
OrderedDict([(3, 't'), (1, 'a'), (2, 'b')])

Also as another efficient way if it's not necessary to use a dictionary you can use a deque that allows you to append from both side :

>>> from collections import deque
>>> d=deque()
>>> d.append((1,'a'))
>>> d.append((4,'t'))
>>> d
deque([(1, 'a'), (4, 't')])
>>> d.appendleft((8,'p'))
>>> d
deque([(8, 'p'), (1, 'a'), (4, 't')])
like image 42
Mazdak Avatar answered Sep 27 '22 01:09

Mazdak