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

Rabih Atallah
Rabih Atallah
3,405 Points

Please is there a simpler script to solve this challenge

I feel my script is too long or complicated to return a list of tuples from L and S

zippy.py
# combo(['swallow', 'snake', 'parrot'], 'abc')
# Output:
# [('swallow', 'a'), ('snake', 'b'), ('parrot', 'c')]
# If you use list.append(), you'll want to pass it a tuple of new values.
# Using enumerate() here can save you a variable or two.
def combo(L,S):
    X = []
    B = []
    i = 0
    while i < len(L):
        X.append(L[i])
        X.append(S[i])
        t = tuple(X)
        B.append(t)
        X.remove(L[i])
        X.remove(S[i])
        i = i + 1
    return B

2 Answers

Jose Soto
Jose Soto
23,407 Points

This is a simpler method for the same result:

def combo(list, string):
    result = []
    for i, item in enumerate(list):
        result.append((item, string[i]))
    return result
Rabih Atallah
Rabih Atallah
3,405 Points

Thank Jose for your help! I appreciate it

Well the simplest way would be to use the zip function but that may negate the point of the exercise.

https://docs.python.org/3/library/functions.html#zip

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You should a) not use zip in the exercise ;) and b) link to Python 3 docs instead of Python 2.

Good catch on the python 2 docs. And yep, zip kinda eliminates the purpose of the exercise. That's what I get for answering questions before waking up the brain :P

PS You look familiar. Wonder if I've seen you at a board game night or tech meetup in Portland.