Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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'))