list dictionary elements and a list in a line


Hi, I'm doing a test with lists and dictionaries, I got to a point where I had something like that.

datos = {'key1':'a','key2':'b','key3':'c'}
dic = ["alfa","beta","gama"]

My intention is to print


I tried it with a 'for' cycle but for each element of the dictionary I printed four times one of the list

for i in datos:
   t = datos[i]
   for l in dic:
      print t,l

Among several ways I've tried, one track ..

asked by ellipsys 30.06.2017 в 08:20

1 answer


When doing a loop inside another loop you repeat the action more times than you want.

I recommend using the command zip() to iterate pairs of dictionaries:

datos = {'key1':'a','key2':'b','key3':'c'}
dic = ["alfa","beta","gama"]

for i, j in zip(sorted(datos), dic):
    print (datos[i], j)
answered by 30.06.2017 в 08:34