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 trialigsm '
10,440 Pointscombo(), tuples and enumerate challenge. Please help.
Hi!
I do not understand how can we make a new tuple here that combines both iterables? Enumerate already outputs a tuple (index and value), as I understood - tuples are immutable. How can I use enumerate (index) here to build a new list of tuples that combine two iterables ? Thanks
# combo(['swallow', 'snake', 'parrot'], 'abc')
# Output:
# [('swallow', 'a'), ('snake', 'b'), ('parrot', 'c')]
# If you use list.append(), you'll want to pass it a tuple of new values.
# Using enumerate() here can save you a variable or two.
def combo(i1, i2):
the_list = []
for step in enumerate(i2):
3 Answers
igsm '
10,440 PointsSorry Kenneth, I am still little bit confused. I understand what output I should get but I cannot get how can I concatenate two iterables into the tulpe by using for loop and enamurate...
Kenneth Love
Treehouse Guest TeacherYou have a good start.
Tuples are immutable, true, but they're only immutable once they've been created. You can, of course, put into them whatever you'd like. So for each step of your for
loop, you make a new tuple w/ two items in it, one from each iterable, and then put that tuple into your the_list
variable. No tuples will be harmed in the running of this loop!
igsm '
10,440 PointsOk. Thanks Kenneth. I got it now.
Kenneth Love
Treehouse Guest TeacherKenneth Love
Treehouse Guest TeacherLet's take what you have:
Now, in your
for
loop,step
is going to have two things in it. An index value for where we are in the loop (0 the first time, 1 the second, 2 the third, and so on) and the value of the loop. So looking at the example, the first time through yourfor
loop,step
is going to be(0, 'a')
(enumerate()
gives us tuples).So how would you use
step
's contents to get the same indexed item out ofi1
? Maybei1[step[0]]
? It'd probably be easier if we unpacked the tuple fromenumerate
, huh? Let's change your code a bit.OK, now we have two variables that are named what the two things in the tuple are. So how would you use
index
to get the same indexed item out ofi1
? Again, probablyi1[index]
, right? Now puti1[index]
andvalue
into a tuple and append that tuple tothe_list
. Then you're done!