Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Tuples Combo

Chaya Feigelstock
Chaya Feigelstock
2,429 Points

Why is this code not working?

def combo(item_1, item_2): new_list = [] for item_a, item_b in item_1, item_2: new_list.append[item_a, item_b]

return new_list
combo.py
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]

def combo(item_1, item_2):
    new_list = []
    for item_a, item_b in item_1, item_2:
        item_a, item_b.append[new_list]
    return new_list

1 Answer

kyle kitlinski
kyle kitlinski
5,619 Points

Your code is basically trying to unpack the list item_1 into item_a and item_b and erroring before item_2 I'd reccomend running it in a terminal you'll get the output :

ValueError: too many values to unpack (expected 2)

def combo(item_1, item_2):
    new_list = []
    # for item_a, item_b in item_1, item_2:
    for index, value in enumerate(item_1): # using enumerate to get the index and the value from the first list and use  the index in the second list
        # item_a, item_b.append[new_list] # this is appending the new list to item_b
        new_list.append((value, item_2[index]))
    return new_list

# alternatively with comprehensions
def combo_comprehension(item_1, item_2):
    return [(value, item_2[index]) for index, value in enumerate(item_1)]

# using zip
def combo_zip(item_1, item_2)
    return zip(item_1, item_2)

print(combo([1, 2, 3], 'abc'))