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 Combo

Alex Rendon
Alex Rendon
7,498 Points

I can't solve this task, Who can explain me please?

I don't understand very good this topic. Help me please.

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.

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You want to get two items, one from each iterable, and you want them to be at the same index position in each one. Like in the example, both "swallow" and "a" are in the 0th index. "snake" and "b" are in the 1th, and so on.

The suggestion is to use enumerate() because it'll give you back your index with your value. You could do it with a counting variable, too, but that's not as elegant/Pythonic.

Mustafa BaลŸaran
Mustafa BaลŸaran
28,046 Points

the combo function, takes two arguments: a list and a string. In the function, you can begin by declaring an empty list variable, let us say output. We will fill this up with the tuples in the next steps. Then in a for loop which iterates over the list argument with a built-in enumerate function, you can access both the index positions and individual list items. This allows you to create tuples consisting of list items and one-item slices of the string. in the next line, you can append these tuples to the empty output list variable that you have initially declared. Outside the for loop, you return output. Done.

Please give it a shot first before reading the code below.

arg1 = ['swallow', 'snake', 'parrot']
arg2 = 'abc'

def combo(arg1, arg2):
    output = []
    for item in enumerate(arg1):
        a_tuple = (item[1], arg[item[0]])
        output.append(a_tuple)
    return output

I hope that this helps.