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:
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([1, 2, 3], 'abc') # Output: # [(1, 'a'), (2, 'b'), (3, 'c')] def combo(iter1, iter2): return list(zip(iter1, iter2))
Thank you very much!
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:
def combo(*args): a = args 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)
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
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.
enumerateis 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
xrangein Python 2.x)
End of the day, who knows? not me, for sure :)