iterate through an irregular nested list in Python

Souvik Dutta souvik.viksou at gmail.com
Fri Mar 6 21:48:12 EST 2020


A better way would be to just do
nestedl = [[2], [], [], [l, b, n]]    #nested list
for a in nestedl:       #taking all the sublist

      for b in a:   #iterating through the sub
             print(b)

Though it is easy the only limitation is that it works only if the elements are lists. Which we can be easily overcome by using an if statement to check the elements type.


On Fri, 6 Mar, 2020, 6:30 pm , <sinndhhu at gmail.com> wrote:

> Hi All,
> I am new to python.
> I have a irregular nested lists in a list.
> Please help me to iterate through each element.
>
> Thanks much in advance.
>
> Sample Ex
>
> aList = [[2,'jkj'],[],[],['kite',88,'ooo','pop','push','pull'],['hello']]
>
> expected output:
> 2,jkj,,,kite,88,ooo,pop,push,pull,hello
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

On Fri, 6 Mar, 2020, 6:30 pm , <sinndhhu at gmail.com> wrote:

> Hi All,
> I am new to python.
> I have a irregular nested lists in a list.
> Please help me to iterate through each element.
>
> Thanks much in advance.
>
> Sample Ex
>
> aList = [[2,'jkj'],[],[],['kite',88,'ooo','pop','push','pull'],['hello']]
>
> expected output:
> 2,jkj,,,kite,88,ooo,pop,push,pull,hello
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>


More information about the Python-list mailing list