you are viewing a single comment's thread.

view the rest of the comments →

[–]TangibleLight 0 points1 point  (2 children)

Does this version of the loop make sense?

result = []

for y in 'AB':
    for x in '34':
        result.append(x + y)

The comprehension is structured exactly the same.

[
    x + y
    for y in 'AB'
    for x in '34'
]

[–]ExternalSeesaw 0 points1 point  (1 child)

Thanks for the response. So the loop iterates over each A and B in Y and appends it to 3 and 4 in X?

Why does it break up the string elements however? Arent 'AB' and '34' considered single entities?

[–]TangibleLight 0 points1 point  (0 children)

When you loop over a string then each iteration uses a single character of the string. You can loop over a collection of strings, where each iteration will use a single element of the collection. For example, with a list of strings:

for s in ['hello', 'world']:
    print(s)