Python beginner needs help with a code

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.

Python relies on indentation for scoping. Please see the following.

Having your code retain its original formatting in your posts by formatting it correctly will make it much easier for others to help you.

Thanks for the tip. Here’s the code:

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)

I changed two variable names (in the for loops)
for clarification (so that the variable that’s the iterator doesn’t get mixed up with what’s iterated)
and it seems to run fine.

for cities in cities: changed to for city in cities: (and inside the loop, cities changed to city)
for days in days: changed to for day in days: (and in the next line, days changed to day)

days = ['Monday', 'Wednesday', 'Friday'] cities = ['Shelbyville', 'Springfield'] def day_city_pair(days, cities): final_list = [] temp_list = [] for city in cities: for day in days: day_city_pair = day, city temp_list.append(day_city_pair) if len(temp_list) == 3: final_list.append(temp_list) temp_list = [] print(final_list) day_city_pair(days, cities)

I’m more interested in why is this format needed?

1 Like

Seems like a dictionary would be more efficient:
{"city": ["day1", "day2", "day3"]}

I was working on an assignment that compares the type of music the people in the two cities listen to on different days. The output I wanted was supposed to be the input for another function. I could have matched the city with the day manually to complete the assignment but I just wanted to find a way of simplifying the task as I would have done if I had more cities and days to compare.

That solves the problem! Thanks for your help.