Adding elements from a list to a dictionary in Python3

Asked

Viewed 2,542 times

1

Good afternoon, I’m having trouble adding elements of a list to a dictionary.

The list elements 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 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?

1 answer

5

Your code generates a wrong result because the value of y will always match the elem in the list and thus will always be the same value. To implement something of this kind, you need 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 function implementation recommendation pairwise function-based itertools.tee

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

However, note that the second element of the first pair will be the first element in the second pair and that is not what we need. This happens because a and b are different iterators. By making it the same iterator, we get the desired output:

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

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 on Repl.it | Ideone | Github GIST

Browser other questions tagged

You are not signed in. Login or sign up in order to post.