0

I have a list of variables and each variable in that list contains a list of its own. Here is a little example:

var1 = ['apple', 'orange']
var2 = ['teddy bear', 'turtle']
var3 = ['bmw', 'kia']

var_list = [var1, var2, var3]

Then I want to have a loop that iterates over the lists and then another loop that iterates over the items themselves in the list taken from the initial loop. My first instinct was to do this:

for var in var_list: # to select the variable that I want to access the table of
       for items in var: # to select each item inside the list
             block of code 

but this inevitably resulted in the first loop iterating over the list of the first variable when I intended the code to do that in the second loop.

Any idea on how to have a loop iterate through a list of variables, and then a second nested loop to iterate through that variable's items?

Slade
  • 17
  • 3
  • If I replace your "block of code" with `print(items, end=' ')` I get "apple orange teddy bear turtle bmw kia". Could you explain what you want to get instead? – Matthias Feb 18 '23 at 14:47
  • Your code does exactly what you expected it to do. Maybe the `block of code` is doing something confusing. Just replace it by `print(items)` and you'll see it works. – Ignatius Reilly Feb 18 '23 at 14:47

0 Answers0