0

I have two lists:

list1 = ['r', '8', 'w', 'm', 'f', 'c', 'd',...]
list2 = ['AA', 'AB', 'AC', 'AD', 'AE', 'AF',...]

I wish to put both of them into a dictionary such that:

{'r':'AA', '8':'AB', 'w':'AC', 'm':'AD',...}

I have tried using:

dictionary = dict(zip(list1, list2))

However, I believe this function does some sort of strange ordering as I get the following output if I print "dictionary":

{'1': 'BE', '0': 'EB', '3': 'CE', '2': 'FE', '5': 'DB',...}

Why is this, and how would the desired output be produced?

Matthew Frost
  • 578
  • 5
  • 13

4 Answers4

6

Dictionaries are an unordered data structure. The items from your pair of lists will still be paired correctly, but the ordering will be lost.

If you required the ordering of the lists to be preserved in the dict, you can use an OrderedDict instead. Note that OrderedDict is not as performant as a regular dict, so don't use them unless you actually need the ordering.

>>> from collections import OrderedDict
>>> list1 = ['r', '8', 'w', 'm', 'f', 'c', 'd']
>>> list2 = ['AA', 'AB', 'AC', 'AD', 'AE', 'AF']
>>> OrderedDict(zip(list1, list2))
OrderedDict([('r', 'AA'),
             ('8', 'AB'),
             ('w', 'AC'),
             ('m', 'AD'),
             ('f', 'AE'),
             ('c', 'AF')])
wim
  • 338,267
  • 99
  • 616
  • 750
0

Python dictionary, how to keep keys/values in same order as declared?

Dictionary is not meant to be ordered. Use orderedDict for this purpose

Community
  • 1
  • 1
Aman gupta
  • 39
  • 4
  • 1
    if you are just going to give a link to another SO question, flag as a duplicate instead of posting an answer :) – R Nar Dec 09 '15 at 17:44
  • Then avoid answering altogether in cases like this. This should really be a comment. Once you have enough rep (25 I think) you can add a comment on any question/answer. – That1Guy Dec 09 '15 at 17:48
0

Dictionaries behave as unordered lists, so it has nosense trying to order them as [explained in python docs](https://docs.python.org/2/tutorial/datastructures.html#dictionaries. However, using a loops to assign values)

However, you can append them to an empty dictionary using loops, like this:

myDict = {}
list1 = ['r', '8', 'w', 'm', 'f', 'c', 'd',...]
list2 = ['AA', 'AB', 'AC', 'AD', 'AE', 'AF',...]

#To prevent different length issues,
#  set max and min length of both lists
max_len = max(len(list1), len(list2))
min_len = min(len(list1), len(list2))
list1IsBigger = len(list1) > len(list2)


# We loop using the min length
for n in xrange(0, min_len):
    myDict[list1[n]] = list2[n]

#From here, do whatever you like with
# remaining items from longer list

if len(list1) != len(list2):
    for n in xrange(min_len, max_len):
        if list1IsBigger:
            #Do whatever with list1 here
        else:
            #Do whatever with list2 here
SebasSBM
  • 860
  • 2
  • 8
  • 32
0

This might give you a proper Dictionary without any brackets, mentioned in the comment above, Simple looping would be sufficient

list1 = ['a','b','c','d']
list2 = ['1','2','3','4']
dictionary = {}

def MakeIntoDictionary(List1, List2, Dictionary):
    minlength = min(len(List1), len(List2))
    for x in range(0, minlength):
        Dictionary[List1[x]] = List2[x]

MakeIntoDictionary(list2, list1, dictionary)