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

redo of combo() after completing intermediate python track

I don't have a question... I just had two things to add, and felt it appropriate to post here:

1) My one-line completely Pythonic solution is as such:

combo.py
def combo(iter1, iter2):
    return [(iter1[i], iter2[i]) for i, _ in enumerate(iter1)]

2) I cracked up when I ran this code, and the message returned was:

** Bummer: Don't use zip()! I know it exists but the point of this challenge is to solve the problem yourself. **

Anyway, just wanted to share a good laugh :D

combo.py
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]
def combo(iter1, iter2):
    return list(zip(iter1, iter2))

Thank you very much!

why did you use enumerate instead of range?

Erik Burmeister
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Erik Burmeister
Python Web Development Techdegree Graduate 17,108 Points

Mark, that is pretty funny. I was trying to solve the problem just now and I had heard in the past about zip() and thought it would work just fine here, however, I didn't get that message even though my code explicitly uses zip().

my code

def combo(*args): a = args[0] b = args[len(args)-1] x = list(zip(a,b)) return x

It marks it wrong though.

2 Answers

Hi Erik, thanks for responding. Weird, right? :)

I don't know what to tell you about your experience, unfortunately, because I've copied your code, and I get the don't use zip() message:

combo.py
def combo(*args):
    a = args[0] 
    b = args[len(args)-1] 
    x = list(zip(a,b)) 
    return x

However, the good news is, if I run it in my local environment, it completely works! I stick this in afterwards:

if __name__ == '__main__':
    test1 = combo([1, 2, 3], 'abc')
    print(test1)
Erik Burmeister
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Erik Burmeister
Python Web Development Techdegree Graduate 17,108 Points

Oh, wow! How strange. I actually reloaded the challenge and that time it did give me that error message. Glad it works!

I kinda know what that is but not sure what it means. Still learning. Haha.

Hi @rainmaker, I appreciate the question, and it's a great one. I'm not a highly experienced Pythonista. I've read joel grus' book data science from scratch: first principles with python. According to him:

# more pythonic:
for i, _ in enumerate(iterable):
    pass

# less pythonic:
for i in range(len(iterable)):
    pass

So I quickly jumped on stackoverflow just now and I see it may be up to debate: many others would perhaps agree with you here, claiming range(len(iterable)) is more readable.

One note I saw: for objects with no support for len(), then range(len(iterable)) is prone to break. That same site also says:

Using xrange with len is quite a common use case, so yes, you can use it if you only need to access values by index.

Then another site mentions this:

range(len()) only works with countable, indexable objects.

enumerate is faster when you want to repeatedly access the list/iterable items at their index. When you just want a list of indices, it is faster to to use len() and range (xrange in Python 2.x)

End of the day, who knows? not me, for sure :)