Let's say I have a list:
x = ['abc', 'd', 'efgh']
I am trying to create a function so that its desired output would return:
a d e b f c g h
Which is essentially taking the first characters of each element and then skipping onto the next element if there is no index in that area.
Is there an alternative way of doing this w/o using itertools or the zip function?
I tried doing:
for i in x:
print(i[0], i[1], i[2]....etc)
But that only gives me an error since the second element of the list exceeds the range.
Thank you!
Sure... Take a close look and try to understand what is going on here...
out = []
biggest = max(len(item) for item in x)
for i in range(biggest):
for item in x:
if len(item) > i:
out.append(item[i])
rather than out
, I would consider yield
to return the items in a generator.