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

Create a function named combo that takes two iterables. These could be dicts, tuples, lists, strings...

dicts do not iterate the same way that other iterables do. Also one of the ways that this challenges passes is with indexes. This makes me think that this is probably not a practical challenge? but I don't really know, I could be missing something.

Hi again Alex. I think I pinpointed what was bothering me about the challenge.

# this code passes the challenge

def combo(iter1, iter2):
    tup_list = []
    for index, item in enumerate(iter1):
        tup_list.append((iter1[index], iter2[index]))
    return tup_list

# unless, if I pass in a dict (working local) I get a key error.  
# Which makes sense, cause index is not a key
# I would think my code
# should NOT pass the challenge

3 Answers

Hi John,

I wondered too about the practicality of doing this with dictionaries. Because as you mentioned, you don't know what key is going to be combined with which item in the other iterable.

Related to your question, you're correct that an indexed based solution isn't going to work if dicts are passed in, or any other non-ordered iterable.

Kenneth recently updated the challenge tester to not test dictionaries which is why your code passes. You can read more about that here: https://teamtreehouse.com/community/works-in-ide-not-in-challenge

I'll see if the instructions can be updated to reflect this.

Thanks Jason, for fun I'm working on a solution that would test for a dict and respond accordingly. But your response helps me know I might not be crazy. :D

I think it was changed because at this point we probably don't know enough to properly handle any iterable. So no, you're not crazy. :)

If you get stuck on making it work for any iterable you can check out the solution posted by Umesh Ravji in the link I gave.

Also, the idea I had was to convert each argument to a list using list(). Then the solution you posted with enumerate should work. I didn't fully test this though.

I do know that if you convert a dictionary to a list it will take all the keys and make a list out of them. It also seems like it will work for sets too.

So that's something you could explore.

>>> list({"fruit": "orange", "vegetable": "squash"})
['vegetable', 'fruit']
>>> list({1, 2})
[1, 2]
>>>
Umesh Ravji
Umesh Ravji
42,386 Points

Hey Jason Anello, using iterators no longer appears to work for this question, but converting them to lists works fine.

Here's some examples of what the function should do:

Let's say we are in the Python Shell:

(this isn't my real age)

>>> combo({'name': 'Alex', 'age': 25, 'language': 'Python'}, 'abc')
[('name', 'a'), ('age', 'b'), ('language', 'c')]

>>> combo('abc', 'xyz')
[('a', 'x'), ('b', 'y'), ('c', 'z')]

>>> combo({'name': 'Alex', 'age': 25, 'language': 'Python'}, {'name': 'John', 'age': 23, 'language': 'Python'})
[('name', 'name'), ('language', 'language'), ('age', 'age')]

As you see, if one iterable is a dictionary, you take its keys :smile:

I hope this answer gives you an idea. ~Alex

NOTE: This might not be correct, but it has a high chance since the zip function does it this way.

Hey Alex, thanks for responding. Don't the keys get appended in a random order? This makes me think that the practicality of merging a list that would append in order, with a dict that will append randomly, of little real world value?

Actually, I just realized that the order of the keys in the returned value (with zip) are random :smile:

I guess it's OK to leave that order random.

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Hey y'all! Sorry for the confusion. I've updated this challenge a couple of times, trying to find the sweet spot for it, and I forgot to change the directions last time. You do not have to do anything with dict in this challenge. The directions now don't mention dict at all.

Thanks for bringing it up, sorry for the confusion, and good luck with the course!