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

Hussein Amr
Hussein Amr
2,461 Points

I have no idea how to solve this

any ideas?

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

5 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Here are the steps:

  • Create a function named combo that takes two ordered iterables: def combo(iter1, iter2):
  • Create a list of tuples, where each tuple contains one item from each iterable argument
    • This will, perhaps, need a loop to gather the items from both iterables and create the tuples
  • Return the list of tuples

Comments in the code give an example of the call and return values:

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

Post back if you need more help. Good luck!!

Hussein Amr
Hussein Amr
2,461 Points

Chris Freeman take a look at my code and tell me what's wrong

def combo(a, b):
    list = []
    for k in a and v in b:
        list.append((k , v))
    return list
Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

You cannot have two in statements in the for loop.

One solution involves using enumerate(a)to get an index and one item from a, then use the index to get the matching item from b using b[index]. See more about enumerate in the docs.

Hussein Amr
Hussein Amr
2,461 Points

Chris Freeman so how do i use a as an index to b after i enumerate it?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

As mentioned in the docs, enumerate will return an index and an item from a:

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]

# example usage
>>> for index, season in enumerate(seasons):
...     print(index, season)
...
0 Spring
1 Summer
2 Fall
3 Winter

So, index could be used to reference b, as in, b[index]

Hussein Amr
Hussein Amr
2,461 Points

Chris Freeman ok so i understood what you're saying but I can't really apply it the right way

def combo(a, b):
    list = []
    for index, letter in enumerate(a):
        print(index, letter)
    list_cont = list.append((a , b[index]))

    return list_cont
Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Getting closer.

  • The print statement is not needed. (I used it as an example only)
  • don't use list as a variable name. It is a built-in object type. By assigning to list you make it a local object which can mess up other usages of list().
  • append() operates on a list in-place and returns "None", so list_cont would only be None. Remove the assignment and only use the append method directly on <what every your rename `list` to>.
  • indent the append to be inside the for loop
  • the first argument to the append should be letter and not the full list a.
  • return <what every you rename `list` to>

That should do it. Get trying!!

Hussein Amr
Hussein Amr
2,461 Points

Chris Freeman I finally got it Chris! Thank you very much. I'm really disappointed though because i wasn't even close to the answer and it usually takes me a whole lot of time to get through code challenges :/

Hussein Amr
Hussein Amr
2,461 Points

Chris Freeman I don't get how the b[index] thing worked. can you further explain it?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

b is the second iterable so b[index] uses the index to look up the corresponding item in b that was in a.

My question is why they blocked the zip method dangit

Chris Freeman
Chris Freeman
Treehouse Moderator 68,457 Points

Yes. Using zip makes the solution too easy. The point of the exercise is to learn to iterate over lists.