Home > database >  How to choose first element from one list and the corresponding element from the other nested list
How to choose first element from one list and the corresponding element from the other nested list

Time:10-22

I have a two lists.
One is just a regular list where the elements are just numbers and the other list is a nested list and elements are urls for pictures.

For example,

#a regular list
id=[100, 200, 300, ...] 

#a nested list
urls=[['http://www.a.jpg','http://www.b.jpg','http://www.c.jpg'],
['http://www.dd.jpg','http://www.ee.jpg'],
['http://www.fff.jpg','http://www.gggg.jpg','http://www.hhhh.jpg']] 

My goal is to use a for loop and iterate through each element from nested list for each element in a regular list. For example, when I build a loop going through each id, I want to also print out the corresponding element within the nested list.

Expected code for this would be:
for i in id:
    #here I need to print out each element from the nested list
...
Expected output:
100, ['http://www.a.jpg','http://www.b.jpg','http://www.c.jpg']
200, ['http://www.dd.jpg','http://www.ee.jpg']
300, ['http://www.fff.jpg','http://www.gggg.jpg','http://www.hhhh.jpg']

The other parts of the codes for saving images from url and putting into a local computer is almost there but I keep iterating through a wrong elements or duplicates. I've been spending so much time..please can someone help me with this?

CodePudding user response:

How about something like (e.g. in Python)

for i in range(0, len(id)):
    print(id[i], "," , urls[i])
  • Related