How to add elements of a list in a dictionary in Python3

1

Good afternoon, I'm having trouble adding elements from a list into a dictionary.

The elements of the list to put in the dictionary are:

['Joao', '83889023', 'Maria', '81944356', 'Marcos', '32258899', 'Ana', '88235423', 'George', '1254345', 'Rafaela', '8899345671', 'Pedro', '83223345', 'Aline', '842234565', 'Carlos', '83554463', 'Julia', '13565446', 'Murilo', '23543646', 'Mayra', '233253425', 'Italo', '842142543', 'Rita', '3253464457', 'Aldo', '77443456', 'Raquel', '8384423553', 'Henrique', '88342235', 'Joyce', '987676342', 'Daniel', '3253456346', 'Livia', '325346634', 'Pablo', '87461723', 'Carla', '87351236']

The problem is: In the dictionary the key will have to be the number and the value will have to be the name.

What I tried was:

dic = {}
x = 0
y = 0
for elem in dados:
    dic[elem] = dados[y]
    x = x + 1
    y = y + 1
print(dic)

But what I get is:

{'Joao': 'Joao', '83889023': '83889023', 'Maria': 'Maria', '81944356': '81944356', 'Marcos': 'Marcos', '32258899': '32258899', 'Ana': 'Ana', '88235423': '88235423', 'George': 'George', '1254345': '1254345', 'Rafaela': 'Rafaela', '8899345671': '8899345671', 'Pedro': 'Pedro', '83223345': '83223345', 'Aline': 'Aline', '842234565': '842234565', 'Carlos': 'Carlos', '83554463': '83554463', 'Julia': 'Julia', '13565446': '13565446', 'Murilo': 'Murilo', '23543646': '23543646', 'Mayra': 'Mayra', '233253425': '233253425', 'Italo': 'Italo', '842142543': '842142543', 'Rita': 'Rita', '3253464457': '3253464457', 'Aldo': 'Aldo', '77443456': '77443456', 'Raquel': 'Raquel', '8384423553': '8384423553', 'Henrique': 'Henrique', '88342235': '88342235', 'Joyce': 'Joyce', '987676342': '987676342', 'Daniel': 'Daniel', '3253456346': '3253456346', 'Livia': 'Livia', '325346634': '325346634', 'Pablo': 'Pablo', '87461723': '87461723', 'Carla': 'Carla', '87351236': '87351236'}

How do I solve this problem?

    
asked by anonymous 14.06.2018 / 20:36

1 answer

5

Your code generates a wrong result because the value of y will always match the index of elem in the list and thus will always be the same value. To implement something like this, you have to scroll through the list every two elements:

dic = {}
y = 1
for elem in dados[::2]:
    dic[dados[y]] = elem
    y = y + 2
print(dic)

But a simpler solution is presented below.

Python has a recommendation for the implementation of the pairwise function based on itertools.tee / a>

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

However, notice that the second element of the first pair will be the first element in the second pair and that's not what we need. This happens because a and b are different iterators. When we make it the same iterator, we will have the desired output:

def pairwise(iterable):
    "s -> (s0,s1), (s2,s3), (s4, s5), ..."
    it = iter(iterable)
    return zip(it, it)

In this way, we just do:

name_map = {number: name for name, number in pairwise(data)}

To get the result:

{
    '83889023': 'Joao', 
    '81944356': 'Maria', 
    '32258899': 'Marcos', 
    '88235423': 'Ana', 
    '1254345': 'George', 
    '8899345671': 'Rafaela', 
    '83223345': 'Pedro', 
    '842234565': 'Aline', 
    '83554463': 'Carlos', 
    '13565446': 'Julia', 
    '23543646': 'Murilo', 
    '233253425': 'Mayra', 
    '842142543': 'Italo', 
    '3253464457': 'Rita', 
    '77443456': 'Aldo', 
    '8384423553': 'Raquel', 
    '88342235': 'Henrique', 
    '987676342': 'Joyce', 
    '3253456346': 'Daniel', 
    '325346634': 'Livia', 
    '87461723': 'Pablo', 
    '87351236': 'Carla'
}

See working at Repl.it | Ideone | GitHub GIST

    
14.06.2018 / 20:56