Iterating over an empty list within a list - python -


take following code example:

a = [['james dean'],['marlon brando'],[],[],['frank sinatra']]  n = 0  in a:     print a[n][0]     n = n + 1 

i seem getting error index value:

indexerror: list index out of range 

how skip on empty lists within list named a?

you can check if list empty or not, empty lists have false value in boolean context -

for in a:     if i:         print a[n][0]     n = n + 1 

also, instead of using n separately, can use enumerate function , returns current element index -

for n, in enumerate(a):     if i:         print a[n][0] # though - print i[0] 

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -