Python Forum

Full Version: Need help with dictionary!
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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)
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.
(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'.
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))