Using a for-loop, in another for-loop, to iterate through a list of lists

I'd like to write a set of for-loops that iterate through a list of lists as shown below.

pathways = ['pathway_1','pathway_2']

pathway_1 = ['gene_A', 'gene_B']
pathway_2 = ['gene_C', 'gene_D']

for i in pathways:
    for ii in i:
          print(ii)

I'd like the output to look like this:

gene_A
gene_B
gene_C
gene_D

but currently generates this:

p
a
t
h
w
a
y
_
1
p
a
t
h
w
a
y
_
2

Is there a simple way of achieving this?

1 answer

  • answered 2021-11-29 05:13 RoseGod

    In the outside loop you are looping over a string and not a list, change the code like this:

    pathway_1 = ['gene_A', 'gene_B']
    pathway_2 = ['gene_C', 'gene_D']
    
    pathways = [pathway_1, pathway_2]
    
    for i in pathways:
        for ii in i:
              print(ii)
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum