-2

I am learning python now and need a solution for this problem!

city_indices = list(range (0,len (cities)))

city_indices [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

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

Your task is to assign the variable names_and_ranks to a list, with each element equal to the city name and it's corresponding rank. For example, the first element would be, "1. Buenos Aires" and the second would be "2. Toronto". Use a for loop and the lists city_indices and city_names to accomplish this.

names_and_ranks = ['change this to different elements'] # make sure the list is empty

names_and_ranks[0] # '1. Buenos Aires'

names_and_ranks[1] # '2. Toronto'

names_and_ranks[-1] # '12. Iguazu Falls'

Thanks for help. I found the solution

names_and_ranks = []
for index in city_indices: 
    names_and_ranks.append(f"{index + 1}. {city_names[index]}")
Morgan
  • 11
  • 2
  • Does this answer your question? [What is the best way to iterate over multiple lists at once?](https://stackoverflow.com/questions/10080379/what-is-the-best-way-to-iterate-over-multiple-lists-at-once) – Chris Jan 10 '20 at 04:07
  • seems like a homework problem of sorts, so i'll give you some hints. `city_indicies[0] = 0`, so `city_indicies[0] + 1 = 1`. Use a for loop and since the length of both lists are the same length, you only need to use one loop. also string concatenation: `myStr = 'a' + 'b' + 'c' = 'abc'`. – Randy Maldonado Jan 10 '20 at 04:09
  • Smells like homework. – High-Octane Jan 10 '20 at 04:22
  • it is not homework , it is an exercise – Morgan Jan 10 '20 at 04:25

1 Answers1

0

Use extend in python to combine lists.

Listing_1 = [1,2,3]
Listing_2 = [4,5,6]
Listing_1.extend(Listing_2)
Listing_1
[1,2,3,4,5,6]

If you really want to use a loop, use like this.

Listing_1 = [1,2,3]
Listing_2 = [4,5,6]
for item in Listing_2:
    Listing_1.append(item)
High-Octane
  • 1,104
  • 5
  • 19