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 (Retired) Tuples Tuples With Functions

fahad lashari
fahad lashari
7,693 Points

Combo Code challenge question

Hi,

I solved this challenge in two different ways. Both deliver the desired result. However I feel like this could be further improved and made more efficient. Please check out both of my solutions and advice me on what I can improve and what I must NEVER do.

kind regards,

Fahad

solution 1:

def combo(list1, list2):
    list = []

    for (item1, item2) in zip(list1, list2):
        list.append((item1, item2))
    return list    
combo([1, 2, 3], 'abc') 

solution 2:

def combo(list1, list2):
    list = []

    for value1, value2 in enumerate(list2):
        list.append((value1+1, value2))
    return list    
combo([1, 2, 3], 'abc')

2 Answers

Steven Parker
Steven Parker
231,275 Points

:point_right: "Solution 2" does not satisfy the challenge requirements.

It might appear to do the same thing when tested using these particular arguments, but it will not do the same thing with other arguments, and it will not pass the challenge. Try pasting it into the challenge yourself and see! Or test both versions using: combo('abc', [1, 2, 3]) or combo('test', 'this').

As far as what to "NEVER" do: Never assume a single test case is enough to verify the proper operation of a function.

The key takeaway is in Steven's answer, but regarding your first soution (which is fine), you can also construct a list from the zip object with the built-in list() method, so the whole function body will be just one line:

def combo(list1, list2):
    return list(zip(list1, list2))
fahad lashari
fahad lashari
7,693 Points

ooo nice! Very clever, thank you