How to iterate through letters in a list
Question:
I am trying to iterate through all the letters in the list, although i am not making much progress.
EX:
L = ["pizza","burger","shawarma","nuggets"]
the program should output
p b s n
i u h u
z r a g
and so on, basically iterating the words by their indexes.
I tried doing it by a for loop like this:
newlist = [i[0] for i in a]
Which only outputs the first letters in the list.
I am wondering about how to iterate it to have all the indexes if it is possible.
Answers:
Try this:
>>> list(zip(*lst))
[('p', 'b', 's', 'n'),
('i', 'u', 'h', 'u'),
('z', 'r', 'a', 'g'),
('z', 'g', 'w', 'g'),
('a', 'e', 'a', 'e')]
# If you want a string with space between chars use "' '.join"
>>> list(map(' '.join, zip(*lst)))
['p b s n', 'i u h u', 'z r a g', 'z g w g', 'a e a e']
Explanation:
# convert string to list of chars
>>> [list(l) for l in lst]
[['p', 'i', 'z', 'z', 'a'],
['b', 'u', 'r', 'g', 'e', 'r'],
['s', 'h', 'a', 'w', 'a', 'r', 'm', 'a'],
['n', 'u', 'g', 'g', 'e', 't', 's']]
# ^ , ^ , ^ , ^ , ^ ^ , ^, ^ NB. If you want to continue this zip for other elements, you need to use 'itertools.zip_longest'
# ^ , ^ , ^, ^ , ^ <- we need 'zip'.
# Now we need 'zip' for gathering ('p', 'b', 's', 'n') , ...
>>> list(zip(*lst))
[('p', 'b', 's', 'n'),
('i', 'u', 'h', 'u'),
('z', 'r', 'a', 'g'),
('z', 'g', 'w', 'g'),
('a', 'e', 'a', 'e')]
There’s no need to use list on the words – iterating over a string yields individual characters:
>>> food = ["pizza","burger","shawarma","nuggets"]
>>> for line in (' '.join(row) for row in zip(*food)):
... print(line)
...
p b s n
i u h u
z r a g
z g w g
a e a e
If you needed to use the longest word’s length, you could use itertools.zip_longest
:
>>> from itertools import zip_longest
>>> for row in zip_longest(*food, fillvalue=' '):
... print(' '.join(row))
...
p b s n
i u h u
z r a g
z g w g
a e a e
r r t
m s
a
To handle the longest word without using zip_longest
, one approach would be to first normalize the words so they are all the same length, and then use the original zip
approach:
>>> longest = max(len(word) for word in food)
>>> normalized = [word + (' ' * (longest - len(word))) for word in food]
>>> normalized
['pizza ', 'burger ', 'shawarma', 'nuggets ']
>>> for row in zip(*normalized):
... print(' '.join(row))
...
p b s n
i u h u
z r a g
z g w g
a e a e
r r t
m s
a
I am trying to iterate through all the letters in the list, although i am not making much progress.
EX:
L = ["pizza","burger","shawarma","nuggets"]
the program should output
p b s n
i u h u
z r a g
and so on, basically iterating the words by their indexes.
I tried doing it by a for loop like this:
newlist = [i[0] for i in a]
Which only outputs the first letters in the list.
I am wondering about how to iterate it to have all the indexes if it is possible.
Try this:
>>> list(zip(*lst))
[('p', 'b', 's', 'n'),
('i', 'u', 'h', 'u'),
('z', 'r', 'a', 'g'),
('z', 'g', 'w', 'g'),
('a', 'e', 'a', 'e')]
# If you want a string with space between chars use "' '.join"
>>> list(map(' '.join, zip(*lst)))
['p b s n', 'i u h u', 'z r a g', 'z g w g', 'a e a e']
Explanation:
# convert string to list of chars
>>> [list(l) for l in lst]
[['p', 'i', 'z', 'z', 'a'],
['b', 'u', 'r', 'g', 'e', 'r'],
['s', 'h', 'a', 'w', 'a', 'r', 'm', 'a'],
['n', 'u', 'g', 'g', 'e', 't', 's']]
# ^ , ^ , ^ , ^ , ^ ^ , ^, ^ NB. If you want to continue this zip for other elements, you need to use 'itertools.zip_longest'
# ^ , ^ , ^, ^ , ^ <- we need 'zip'.
# Now we need 'zip' for gathering ('p', 'b', 's', 'n') , ...
>>> list(zip(*lst))
[('p', 'b', 's', 'n'),
('i', 'u', 'h', 'u'),
('z', 'r', 'a', 'g'),
('z', 'g', 'w', 'g'),
('a', 'e', 'a', 'e')]
There’s no need to use list on the words – iterating over a string yields individual characters:
>>> food = ["pizza","burger","shawarma","nuggets"]
>>> for line in (' '.join(row) for row in zip(*food)):
... print(line)
...
p b s n
i u h u
z r a g
z g w g
a e a e
If you needed to use the longest word’s length, you could use itertools.zip_longest
:
>>> from itertools import zip_longest
>>> for row in zip_longest(*food, fillvalue=' '):
... print(' '.join(row))
...
p b s n
i u h u
z r a g
z g w g
a e a e
r r t
m s
a
To handle the longest word without using zip_longest
, one approach would be to first normalize the words so they are all the same length, and then use the original zip
approach:
>>> longest = max(len(word) for word in food)
>>> normalized = [word + (' ' * (longest - len(word))) for word in food]
>>> normalized
['pizza ', 'burger ', 'shawarma', 'nuggets ']
>>> for row in zip(*normalized):
... print(' '.join(row))
...
p b s n
i u h u
z r a g
z g w g
a e a e
r r t
m s
a