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]}")
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.