Hi guys. A python beginner here who is having a hard time figuring out why this code is not working…
I needed a code that will convert this:
days = [‘Monday’, ‘Wednesday’, ‘Friday’]
cities = [‘Shelbyville’, ‘Springfield’]
into the following output:
[[(‘Monday’, ‘Shelbyville’), (‘Wednesday’, ‘Shelbyville’), (‘Friday’, ‘Shelbyville’)],
[(‘Monday’, ‘Springfield’), (‘Wednesday’, ‘Springfield’), (‘Friday’, ‘Springfield’)]]
Here is the function I made for it:
def day_city_pair(days, cities):
final_list =
temp_list =
for cities in cities:
for days in days:
day_city_pair = days, cities
temp_list.append(day_city_pair)
if len(temp_list) == 3:
final_list.append(temp_list)
temp_list =
print(final_list)
But this is the output I got:
[[(‘Monday’, ‘Shelbyville’), (‘Wednesday’, ‘Shelbyville’), (‘Friday’, ‘Shelbyville’)], [(‘F’, ‘Springfield’), (‘r’, ‘Springfield’), (‘i’, ‘Springfield’)], [(‘d’, ‘Springfield’), (‘a’, ‘Springfield’), (‘y’, ‘Springfield’)]]
The loop runs fine the first time, but when the loop is running the second time, it appears the code is using the characters in ‘Friday’ as the ‘days’.
What’s wrong with the code?
Thanks.