0

This is my code:

def lists_to_dict(coded, plain):
    '''
    (list of str, list of str) -> dict of {str: str}
    Return a dict in which the keys are the items in coded and
    the values are the items in the same positions in plain. The
    two parameters must have the same length.

    >>> d = lists_to_dict(['a', 'b', 'c', 'e', 'd'],  ['f', 'u', 'n', 'd', 'y'])
    >>> d == {'a': 'f', 'b': 'u', 'c': 'n', 'e': 'd', 'd': 'y'}
    True
    '''
    dic = {}
    dic = {key:value for key, value in zip(coded, plain)}
    return dict(dic)

and my output:

>>> {'b': 'u', 'c': 'n', 'e': 'd', 'd': 'y', 'a': 'f'}

can someone please tell me where do I got wrong and help me out please!

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Jason
  • 9
  • 4
  • 4
    you've done it correctly. You do have some redundancies (`d = {}` not needed and `return dic` suffices) but aside from that the output is correct. – Dimitris Fasarakis Hilliard Nov 18 '16 at 08:52
  • 2
    There's nothing wrong. Dicts just are not ordered by their keys when you print them. By the way: You can omit the line `dic = {}`, it gets overwritten by the following line anyhow. – jbndlr Nov 18 '16 at 08:52
  • No errors apparently – MMF Nov 18 '16 at 08:52
  • 2
    `dict(zip(coded, plain))` would be enough in such case – RomanPerekhrest Nov 18 '16 at 08:54
  • @JimFasarakis-Hilliard Thank you for your help! but the output is not in a right order, which should be same as the example shown in the docstring. Also, one more question, how can I return the boolean as the example shows? – Jason Nov 18 '16 at 09:04
  • Dictionaries are not ordered (so don't worry about the order). The boolean is a result of the comparison which checks if your function result compares `True` to `{'a': 'f', 'b': 'u', 'c': 'n', 'e': 'd', 'd': 'y'}` (which it does). – Dimitris Fasarakis Hilliard Nov 18 '16 at 09:06
  • You can use `OrderedDict` (in the `collections` standard library package) if you want to also remember insertion order. – Arthur Tacca Nov 18 '16 at 09:07
  • @JimFasarakis-Hilliard Thank you so much!! It helps a lot! – Jason Nov 18 '16 at 09:13

2 Answers2

1

Either you can try this one...

I have mentioned two ways below..

Python 3.4.3 (default, Sep 14 2016, 12:36:27) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> d = (['a', 'b', 'c', 'e', 'd'],  ['f', 'u', 'n',   'd', 'y'])
>>> dictionary = dict(zip(d[0], d[1]))
>>> dictionary
{'d': 'y', 'c': 'n', 'b': 'u', 'a': 'f', 'e': 'd'}
>>> dict(zip(*d))
{'d': 'y', 'c': 'n', 'b': 'u', 'a': 'f', 'e': 'd'}

dict(zip(*d) use only when list/tuple has length 2. If length is more than 2 then error will occur as

>>> d = (['a', 'b', 'c', 'e', 'd'],  ['f', 'u', 'n',   'd', 'y'],['1','2','3','4'],['5','6','7','8'])
>>> dict(zip(*d))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: dictionary update sequence element #0 has length 4; 2 is required
Rohan Khude
  • 4,455
  • 5
  • 49
  • 47
0

python dictionary are not ordered in nature, use OrderedDict from collections if you need them in order follow the input list

from collections import OrderedDict
def lists_to_dict(coded, plain):
    '''
    Return order dictionary follow order of input list
    '''

    # return dict(zip(coded, plain)) # if the order doesn't matter
    return OrderedDict(zip(coded, plain))


# demo
>>> d = lists_to_dict(['a', 'b', 'c', 'e', 'd'], ['f', 'u', 'n', 'd', 'y'])
>>> d
OrderedDict([('a', 'f'), ('b', 'u'), ('c', 'n'), ('e', 'd'), ('d', 'y')])
>>>
>>> for k,v in d.items():
...     print('{} : {}'.format(k,v))
...
a : f
b : u
c : n
e : d
d : y
Skycc
  • 3,496
  • 1
  • 12
  • 18