Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help with dictionary!
#1
Hello,

I encountered this issue and tried to figured out why this happened that way but was not able to. Can anyone help me with this? The one that I got compared to the presented result below was messed up in term of order. Any help is greatly appreciated.

# [ ] The data list contains information about a company's employees.
# Use the data list and an appropriate loop to create a dictionary of employees.
# Use IDs (as keys) and names (as values).
# Ignore the email addresses for now.

# The created dictionary should look like:
# {57394: 'Suresh Datta', 48539: 'Colette Browning', 58302: 'Skye Homsi', 48502: 'Hiroto Yamaguchi', 48291: 'Tobias Ledford', 48293: 'Jin Xu', 23945: 'Joana Dias', 85823: 'Alton Derosa'}

My code:
data = [["Suresh Datta", 57394, "[email protected]"], ["Colette Browning", 48539, "[email protected]"], ["Skye Homsi", 58302, "[email protected]"], ["Hiroto Yamaguchi", 48502, "[email protected]"], ["Tobias Ledford", 48291, "[email protected]", "Tamara Babic", 58201, "[email protected]"], ["Jin Xu", 48293, "[email protected]"], ["Joana Dias", 23945, "[email protected]"], ["Alton Derosa", 85823, "[email protected]"]]
dictionary = {}
for item in data:
    dictionary[item[1]] = item[0]
print(dictionary)
Reply
#2
What Python version are you using? As of Python 3.6, dictionaries retain order of item insertion. I just ran the code in Python 2.7 (for testing), and the order of item insertion was corrupted.
Reply
#3
(Jul-17-2019, 06:38 PM)hoangthai10788 Wrote: # The created dictionary should look like:
# {57394: 'Suresh Datta', 48539: 'Colette Browning', 58302: 'Skye Homsi', 48502: 'Hiroto Yamaguchi', 48291: 'Tobias Ledford', 48293: 'Jin Xu', 23945: 'Joana Dias', 85823: 'Alton Derosa'}

'look like' is not the same as 'it must be' (at least for me - non-native english speaker). I interpret it as 'structure must be similar' not 'it must be exactly same'.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
You can check for the running Python version and use OrderedDict from collections module.

import sys


major = sys.version_info.major
minor = sys.version_info.minor

if major == 3 and minor >= 6:
    print('Dicts preserve the order in this Python version')
    dictionary = {}
else:
    print('Older Python version, using OrderedDict')
    from collections import OrderedDict
    dictionary = OrderedDict()


def check_order(original_data, dictionary):
    """
    A helper function to test 
    the preserving order of keys in a dictionary
    """
    return all(
        uid == duid[1]
        for uid, duid in
        zip(dictionary.keys(), original_data)
        )


data = [["Suresh Datta", 57394, "[email protected]"], ["Colette Browning", 48539, "[email protected]"], ["Skye Homsi", 58302, "[email protected]"], ["Hiroto Yamaguchi", 48502, "[email protected]"], ["Tobias Ledford", 48291, "[email protected]", "Tamara Babic", 58201, "[email protected]"], ["Jin Xu", 48293, "[email protected]"], ["Joana Dias", 23945, "[email protected]"], ["Alton Derosa", 85823, "[email protected]"]]


for item in data:
    dictionary[item[1]] = item[0]

print(dictionary)
print(check_order(data, dictionary))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020