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
Post a Comment