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

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 35,157 Points

Works in IDE, not in challenge

This works fine in PyCharm. Fails in Code Challenge, again with the very unhelpful "Bummer. Try Again"

1) What am I missing? 2) Could something be done to make the feedback more informative. I normally love the challenges but when I get this feedback message 20 times, I feel like throwing my computer. Can't the tester say something more useful?

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

def combo(myiter1, myiter2):
    combo_list = []
    for index, item in enumerate(myiter2):
        combo_list.append((myiter1[index], item))
    return combo_list

Tagging Kenneth Love

Is this challenge requiring the solution to be coded a certain way?

6 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Hmm, so...this is fun :)

First off, Nancy Melucci, your solution is almost exactly what I wrote in my tests (which don't require the code to look any particular way, btw). But, by using enumerate() on the second iterable instead of the first, you discovered a weird "bug" in my testing.

One of the tests that I send to your combo() function is a dict and a list. So your combo() tries to get an item with a key of 0 from the dict...which doesn't exist. So, time to update the test to prevent that scenario!

Hi Kenneth,

I didn't read the instructions carefully enough to realize it has to work with dicts, sets, etc... that you can't access by index.

Now I'm wondering how we could code this for dicts based on knowledge gained up to this point.

Nancy's solution wouldn't work if the first argument is a dictionary and my suggested enumerate solution won't work if the second argument is a dictionary. The range() solution wouldn't work for similar reasons.

Are you restricting the tests now to tuples, lists, and strings or are dicts still valid?

Would it be valid to convert the arguments to lists using list() and then use the enumerate or range solution?

Umesh Ravji gave an answer here https://teamtreehouse.com/community/combo-4

suggesting that the arguments be converted to iterators. But then don't we have to use generators or maybe repeatedly call next() until a StopIteration error is raised?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Hey Jason Anello,

I don't want to give too much away about how the grader is working, but, yes, it's currently missing any non-ordered iterables.

I don't think it's fair to rely on people using iter() and the like because those haven't been covered. While I'm fine with making y'all look up documentation, most people wouldn't even know where to start for iter() (or zip() for that matter).

I'm going to leave the challenge as-is for now and maybe we'll revisit using it for non-ordered iterables later.

Umesh Ravji
Umesh Ravji
42,386 Points

Hi, Kenneth Love and Jason Anello

I did mention I thought it might not be the best way to do this, but I couldn't see any way (a way that made more 'sense') of achieving the answer without it and without taking advantage of zip :)

def combo(a, b):
    iter_a = iter(a)
    iter_b = iter(b)
    results = []
    while True:
        try:
            results.append((iter_a.next(), iter_b.next()))
        except StopIteration:
            break
    return results

I think I used something to that effect the other day, scary looking now though :p

Hi Nancy,

You were on the right track with your solution and I think it produces the correct result. In my opinion, that code should be passing. This challenge seems to be a little too rigid on how the solution is coded.

If you change it to enumerate over iter1 instead and update the append argument accordingly, it will pass.

def combo(myiter1, myiter2):
    combo_list = []
    for index, item in enumerate(myiter1):
        combo_list.append((item, myiter2[index]))
    return combo_list

This is essentially the same as your solution but this one will pass.

The original way I tried to code this was -

def combo(myiter1, myiter2):
    combo_list = []
    for index in range(len(myiter1)):
        combo_list.append((myiter1[index], myiter2[index]))
    return combo_list

This one uses range to generate the index values and it's only slightly different than your solution but it does not pass either.

I think this is what you were aiming for with your code. Zip works great, but I am not sure if they have taught that yet.

def combo(myiter1, myiter2):
    combo_list = []
    counter = 0

    for item1 in myiter1:
        item2 = myiter2[counter]
        output = (item1, item2)
        combo_list.append(output)
        counter += 1

    return combo_list

Hi Philip,

range() and enumerate() have both been introduced at this point which can make the code shorter by eliminating the need for a manual counter variable

First, (thankfully) this challenge requires only a mere two lines of code.

Second, your code working in your IDE (or Python Shell) it doesn't mean it will pass the challenge.

There's a built-in zip function that does exactly what the challenge is asking for:

def combo(a, b):
    return list(zip(a, b))

(You should be swatting your forehead or laughing out loud when you see this code pass)

I hope this (sort of) helps :P

~Alex

Hi Alexander,

I think it's fine to mention the zip() function as an alternative solution but it wasn't mentioned in the course as far as I can recall.

I think the point of the challenges is to see if you can apply the concepts that were taught in the videos.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Added a test to make sure you don't use zip() :) Yes, it exists and I like to avoid re-inventing wheels, but the point is to solve problem on your own.

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 35,157 Points

Right...he (Love) didn't show us zip. He used enumerate. I would rather use zip but...there you go. I still wish that the compiler would say more. Python is supposed to be "easy" but it hides more of its functionality than other languages. I am not brilliant at any of this, but I have to struggle more with Python, despite the fact I've been told it's the great "beginner's language."

Thanks.

In many challenges, they do not use all of the code they previously used. They are more of an 'are you getting it before we move on' type of quiz. You are correct in assuming the previous video is generally a good place to start.

Nancy Melucci
PLUS
Nancy Melucci
Courses Plus Student 35,157 Points

Thank you kindly Prof. Love. I want to finish this course. I want to be able to code in Python. Someday!