3

Strings are iterable. Lists are iterable. And with a List of Strings, both the List and the Strings can be iterated through with a nested loop.

For Example:

input = [ 'abcdefg', 'hijklmn', 'opqrstu']
for item in input:
     for letter in item:
        print letter

Out:

a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u

It seems we can then iterate within the iteration over the string, i.e. iterate over each letter of 'abcdefg'. Is there a more pythonic way to iterate as above, possibly in one statement?

I've re-read through Learning Python Chapter 4, Chapter 1 of the Python Cookbook, and looked here in python: iterate over each string in a list, and here Displaying and iterating a list within a string?.

Community
  • 1
  • 1
Aaron
  • 132
  • 2
  • 3
  • 17

4 Answers4

7

You can use itertools.chain.from_iterable():

>>> from itertools import chain
>>> input = ['abcdefg', 'hijklmn', 'opqrstu']
>>>
>>> for letter in chain.from_iterable(input):
...     print letter
... 
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • Would you agree or disagree with @ecatmur that using the built in functionality is preferred to importing a library because _explicit is better than implicit_ and why? – Aaron May 30 '14 at 01:39
  • 1
    @Aaron Disagree. There's nothing wrong with using `itertools` (in fact it would be advisable if the situation calls for it, as this one does). Also, `for letter in chain.from_iterable(input)` is as explicit as it gets. If you're worried about that, simply write a comment indicating the intent of the loop. – arshajii May 30 '14 at 02:53
2

Use itertools.chain:

for letter in itertools.chain(*input):
    print letter
chepner
  • 497,756
  • 71
  • 530
  • 681
2

What you've written is already the most pythonic way to do it; there are already two levels of nesting (letters within strings within a list) so it's correct to have two nested for loops.

If you really want to use a single for statement, you can collapse the loops with a generator comprehension:

for letter in (letter for item in input for letter in item):
    print letter
ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • What might I use to determine whether or not I should use one or two for statements? IMO, your single for statement looks elegant. What would be the downside of using your single for statement? – Aaron May 29 '14 at 15:19
  • 1
    @Aaron it's a lot of text in a single statement, and it repeats `letter` more than necessary (3 times as opposed to just 1 with the two for statements). – ecatmur May 29 '14 at 15:23
  • Thank you. In light of the three time repetition of `letter`, would it be preferable to import a library as suggested by others, or to use the built in functionality, such as, a generator? – Aaron May 29 '14 at 15:40
  • 1
    `itertools` should be in the armory of any good Python programmer, but the generator comprehension is clearer about what's going on, so (since *explicit is better than implicit*) I think that wins out. – ecatmur May 29 '14 at 15:48
1

That is good enough, everything else is just an academic puzzle

for item in ''.join(input):
    print item
Abhijit
  • 62,056
  • 18
  • 131
  • 204
vav
  • 4,584
  • 2
  • 19
  • 39
  • 1
    Your solution is printing the characters, but does not answer the question, which clearly asked for iterating. Your "join" is constructing complete string in memory, this could be for large list a problem. – Jan Vlcinsky May 29 '14 at 14:59
  • Not my preferred answer, but I don't really see a reason for down votes given that the question specifically asks about lists of strings, not more generalized situations. – chepner May 29 '14 at 14:59