0

I have a list and I want to put its elements inside a for loop string like this:

my_list = ["Germany", "England", "Spain", "France"]
for this in that:
    do stuff
    print(my_list[0]+ some other stuff)

The output should be like:

Germany + some other stuff
England + some other stuff
Spain + some other stuff
France + some other stuff

How can I loop the indices for string interpolation as well?

Thanks!

Edit: The loop is a bit different. It's more like this:

for foo in bar:
    another_bar = []
    for x, y in foo:
        do stuff
        a = object.method()
        another_bar.append(my_list[0]+a)

I need to put the strings of lists into the 2nd layer of nested loop. Can't use zip here.

2
  • as I understand this is what you want: my_list = ["Germany", "England", "Spain", "France"] for country in my_list: print(country + 'some other stuff') Commented Aug 25, 2017 at 0:41
  • @0x1 I edited your edit to fix indentations. Please ensure it matches your intent. Also, your code needs more structure. For example, index is not defined. Please refer to minimal reproducible example. Commented Aug 25, 2017 at 1:04

3 Answers 3

2

I believe you are assuming that that is the same lenght as my_list. If so, you can use zip to iterate over the two containers in parallel.

my_list = ["Germany", "England", "Spain", "France"]
my_other_list = [' is great at football', ' has good years in rugby', ' has Renaldo', ' is, well...']

def foo(bar):
    return bar + '!'

for country, bar in zip(my_list, my_other_list):
    other = foo(bar)
    print(country + other)

# Output:
# Germany is great at football!
# England has good years in rugby!
# Spain has Renaldo!
# France is, well...!
Sign up to request clarification or add additional context in comments.

1 Comment

Its a nested loop. I cannot use zip here unfortunately. I edited the question.
0

You can use the built-in function zip(). zip allows to process each list in parallel:

Make an iterator that aggregates elements from each of the iterables.

Returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator.

my_list = ["Germany", "England", "Spain", "France"]
for country, this in zip(my_list, that):
    # do stuff
    print(country + that)

If your list are of different size, you can use itertoos.zip_longest:

Make an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted.

from itertools import zip_longest

my_list = ["Germany", "England", "Spain", "France"]
for country, this in zip_longest(my_list, that):
    # do stuff
    print(country + that)

1 Comment

Its a nested loop. I cannot use zip here unfortunately. I edited the question.
0

I hope this can help you.

for index, this in enumerate(that):
    do stuff
    print(my_list[index]+ some other stuff)

2 Comments

Its a nested loop. I cannot use zip here unfortunately. I edited the question.
You say you need an index, and I still feel that enumerate can satisfy you.enumerate

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.