0
for names in city_names:
    iteration_count += 1
    print(iteration_count, names)

I am trying to set the result of this loop equal to a variable, how would I proceed in doing so?

Here is the list I am using in the loop:

city_names = ['Buenos Aires',
 'Toronto',
 'Marakesh',
 'Albuquerque',
 'Los Cabos',
 'Greenville',
 'Archipelago Sea',
 'Pyeongchang',
 'Walla Walla Valley',
 'Salina Island',
 'Solta',
 'Iguazu Falls']
5
  • This would be very bad Python. Also what would be the content of this new variable? Commented Nov 21, 2019 at 23:22
  • 1
    "result of this loop" what is the result of this loop? Commented Nov 21, 2019 at 23:23
  • Can that solve what you want to do: var = zip(city_names, range(len(city_names))) ? Commented Nov 21, 2019 at 23:25
  • 1
    1 Buenos Aires 2 Toronto 3 Marakesh 4 Albuquerque 5 Los Cabos 6 Greenville 7 Archipelago Sea 8 Pyeongchang 9 Walla Walla Valley 10 Salina Island 11 Solta 12 Iguazu Falls Commented Nov 21, 2019 at 23:31
  • 2
    Like this: result = list(enumerate(city_names, start=1))? Commented Nov 21, 2019 at 23:35

3 Answers 3

1

Try this :

 city_names = ['Buenos Aires',
 'Toronto',
 'Marakesh',
 'Albuquerque',
 'Los Cabos',
 'Greenville',
 'Archipelago Sea',
 'Pyeongchang',
 'Walla Walla Valley',
 'Salina Island',
 'Solta',
 'Iguazu Falls']

 result = ''
 iteration_count = 1
 for names in city_names:    
    result = result + str(iteration_count) + ' ' + names + ' '
    iteration_count += 1
 print (result)

Result is as you wanted it to be:

1 Buenos Aires 2 Toronto 3 Marakesh 4 Albuquerque 5 Los Cabos 6 Greenville 7 Archipelago Sea 8 Pyeongchang 9 Walla Walla Valley 10 Salina Island 11 Solta 12 Iguazu Falls 
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the str.join method with a generator expression that iterates through the enumerate generator that outputs city_names with a counter:

output = ''.join(f'{count} {name}\n' for count, name in enumerate(city_names, 1))

output becomes:

1 Buenos Aires
2 Toronto
3 Marakesh
4 Albuquerque
5 Los Cabos
6 Greenville
7 Archipelago Sea
8 Pyeongchang
9 Walla Walla Valley
10 Salina Island
11 Solta
12 Iguazu Falls

Comments

1

In general, to capture what gets printed to the standard output into a variable, you can temporarily override sys.stdout with a StringIO object using the unittest.mock.patch method as a context manager:

from io import StringIO
from unittest.mock import patch

with patch('sys.stdout', new=StringIO()) as output:
    iteration_count = 0
    for names in city_names:
        iteration_count += 1
        print(iteration_count, names)
print(output.getvalue())

This outputs:

1 Buenos Aires
2 Toronto
3 Marakesh
4 Albuquerque
5 Los Cabos
6 Greenville
7 Archipelago Sea
8 Pyeongchang
9 Walla Walla Valley
10 Salina Island
11 Solta
12 Iguazu Falls

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.