I have a list and it contains lists. The inner lists are may be empty. This is the structure:
[[],[a,b],[],[c],[d]]
My code:
for inner in list:
for char in inner:
print char # I want to add indexes of inner lists here
Output is:
abcd
I want the output to be:
2a2b4c5d
The numbers are indexes of inner lists. Is there quick way to do this?
please note that you should not use
listas a variable name, since itshadowsthe built-inlisttype.you see that
enumerate()counts zero-based. you can specify a second argument as start though, to match your one-based expected output:but only do that if neccessary, python counts 0-based by convention.