How to join 2 lists in a dictionary?

Asked

Viewed 156 times

2

I want to take 2 lists and turn them into a dictionary, in which each item has as key the i-th item of the first list and as value the i-th item of the second list.

I made that code so far:

lista_de_compras = ['arroz', 'feijao', 'macarrao']
preco_dos_itens = ['2.00', '3.80', '4.90']

lista_nova = {}

for item in lista_de_compras:
    for preco in preco_dos_itens:
        lista_nova[item] = preco

print(lista_nova)

But when I printo this new listing (which in this case is the dictionary), it applies the items correctly in the keys, but I did not understand the pq it repeat in all only the last price of the item for all the items, and how to solve this problem:

output:

{''rice: '4.90', 'beans': '4.90', 'pasta': '4.90'}

3 answers

7

What you want is the internal function zip() (documentation).

This function takes a list of iterators, and returns a new iterator, in which each element is a tuple contained by a corresponding element of each original iterator.

Example: If the list A = [1,2,3] and the list B=[4,5,6], then zip(A,B) will return (1,4), (2,5), (3,6).

Applying in your case, we have:

lista_de_compras = ['arroz', 'feijao', 'macarrao']
preco_dos_itens = ['2.00', '3.80', '4.90']
dict = {}
for i,j in zip(lista_de_compras, preco_dos_itens):
    dict[i] = j
print(dict) // {'arroz': '2.00', 'feijao': '3.80', 'macarrao': '4.90'}

What you were doing earlier, (i.e., several for nested) did not work because for every single case, you went through the entire second list, which was not the desired.

  • A very good module that facilitates a lot, I at least did not know this function. Thank you so much for sharing.

6

It is unnecessary in python to build iteration loops to create dictionaries from two lists of the same length. Just add the lists with the built-in function zip() and pass the obtained result as an argument to the dictionary constructor:

lista_de_compras = ['arroz', 'feijao', 'macarrao']
preco_dos_itens = ['2.00', '3.80', '4.90']

d = dict(zip(lista_de_compras, preco_dos_itens))

print(d)     #{'arroz': '2.00', 'feijao': '3.80', 'macarrao': '4.90'}

Test the example on Ideone and test in Repl.it

  • 1

    Here to case to say that first was worth more than the best d:. This was the best

1


Jose, the problem is that you nestled the for.

Take as an example this passage:

for preco in preco_dos_itens:
    print(preco)

It will iterate over all prices, printing one at a time. Analogously...

for item in lista_de_compras:
    print(item)

Printa all items. Ok, and when I do this?

for item in lista_de_compras:
    for preco in preco_dos_items:
        print(f'{item} {preco}')

I will be matching all items with all prices. IE, the output will be

arroz 2.0
arroz 3.8
arroz 4.9
feijao 2.0
feijao 3.8
feijao 4.9
macarrao 2.0
macarrao 3.8
macarrao 4.9

To have the right behavior, you need to iterate for the indexes.

numero_de_itens = len(lista_de_compras)
for i in range(numero_de_itens):
  print(f'{lista_de_compras[i]} {preco_dos_itens[i]})'
  • Naslauky’s solution is more idiomatic (or pythonic), but it’s good for you to know how it goes at a lower level of abstraction.

  • 1

    Perfect friend, helped me too much, that’s exactly what my rationing line wanted to get hahaha .

Browser other questions tagged

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