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 trialReynaldo Ponte
669 PointsTuples challenge help. Could you guys help me with reassignments of string?
hi guys i have been stuck on this challenge for almost 2 weeks. I am able to brake up the line
a=(['swallow', 'snake', 'parrot'], 'abc')
with the code;
m=list()
for j in a:
pass
for h in enumerate(j):
m.append(h)
in python 3.xx if i were to put this in to a function
def combo (a):
a=(['swallow', 'snake', 'parrot'], 'abc')
m=list()
for j in a:
pass
for h in enumerate(j):
m.append(h)
m
[(0, 'swallow'), (1, 'snake'), (2, 'parrot'), (0, 'a'), (1, 'b'), (2, 'c')]
I get stuck. I dont know where to go next. for all i know i could be reading the challenge wrong.
could you guys help. thanks?
5 Answers
Kenneth Love
Treehouse Guest TeacherRemember what enumerate()
sends back? It's a tuple of (index, value)
. So catching h
from that gets you the tuple with both values in one object (a tuple). But the challenge wants the item at that index from both lists, so you need to use the index to find the values at that index in each list, put them into a tuple and append them to the list. You're very close!
David Plans
2,756 PointsReynaldo Ponte the trick is to realize that you don't need to iterate over -both- iterable items in what is being passed to the combo function. Read what Kenneth Love outlined above very carefully. Once you have enumerate looping through list1, and its value at each index in the loop, you just need to call the index for list2 at that index to get the value at that index. Here's how I implemented Kenneth's advice:
def combo(iter1, iter2):
mylist = []
tempTuple = ()
for index, value in enumerate(iter1):
tempTuple = (value, iter2[index])
mylist.append(tempTuple)
return mylist
You can in fact get rid of the tempTuple variable and assign directly:
def combo(iter1, iter2):
mylist = []
for index, value in enumerate(iter1):])
mylist.append((value, iter2[index]))
return mylist
I believe Kenneth was hinting at the the inbuilt zip()
function in Python3 with the cheeky assignment title (zippit!), but as he's pointed out elsewhere, we were supposed to figure it out without necessarily using zip.
For what it's worth, using zip would work like:
def combo(iter1, iter2):
mylist = []
for i, j in zip(iter1, iter2):
mylist.append((i, j))
return mylist
and here's a good outline of the difference between enumerate and zip.
Kenneth Love
Treehouse Guest TeacherOK, let's get a bit more generic.
>>> for index, value in enumerate(['dog', 'cat', 'mouse']):
. . . print("The value at {} is {}.".format(index, value))
The value at 0 is dog.
The value at 1 is cat.
The value at 2 is mouse.
I can unpack the tuple that enumerate()
gives back into two variables. I could also have done:
>>> for info in enumerate(['dog', 'cat', 'mouse']):
. . . print("The value at {} is {}.".format(info[0], info[1]))
The value at 0 is dog.
The value at 1 is cat.
The value at 2 is mouse.
That's only one variable, not two, but it's also a bit less clean and Pythonic.
So, using enumerate()
and the index that it gives you, get the thing that's at that index place in both lists. Put those two things into one tuple and put that tuple into a list. Then return that list.
Reynaldo Ponte
669 PointsThanks Kenneth. Your the best.
I am eager to try this but its midnight where I am I will try this tomorrow.
Thank you. so much for your help.
Rey
Reynaldo Ponte
669 Pointshi kenneth.
i am about to give up on this challenge i tried so many ways to get the tuple to return as
[('swallow', 'a'), ('snake', 'b'), ('parrot', 'c')]
but i cant i dont know how to get 'abc' to come together with list ['swallow', 'snake', 'parrot']
in a function.
i tried as follows
a=['swallow', 'snake', 'parrot'], 'abc'
>>> for ind,let in enumerate(a[]):
SyntaxError: invalid syntax
>>> b=list()
>>> for ind,let in enumerate(a[0]):
b.append(let)
for ind,let in enumerate(a[1]):
b.append(let)
print(b)
['swallow', 'a']
['swallow', 'a', 'b']
['swallow', 'a', 'b', 'c']
['swallow', 'a', 'b', 'c', 'snake', 'a']
['swallow', 'a', 'b', 'c', 'snake', 'a', 'b']
['swallow', 'a', 'b', 'c', 'snake', 'a', 'b', 'c']
['swallow', 'a', 'b', 'c', 'snake', 'a', 'b', 'c', 'parrot', 'a']
['swallow', 'a', 'b', 'c', 'snake', 'a', 'b', 'c', 'parrot', 'a', 'b']
['swallow', 'a', 'b', 'c', 'snake', 'a', 'b', 'c', 'parrot', 'a', 'b', 'c']
>>> b
['swallow', 'a', 'b', 'c', 'snake', 'a', 'b', 'c', 'parrot', 'a', 'b', 'c']
>>> del b
>>> b=list()
>>> for ind,let in enumerate(a[0]):
b.append(let)
for ind,let in enumerate(a[1]):
b.append(let)
print(b)
['swallow', 'a', 'b', 'c']
['swallow', 'a', 'b', 'c', 'snake', 'a', 'b', 'c']
['swallow', 'a', 'b', 'c', 'snake', 'a', 'b', 'c', 'parrot', 'a', 'b', 'c']
>>> b
['swallow', 'a', 'b', 'c', 'snake', 'a', 'b', 'c', 'parrot', 'a', 'b', 'c']
>>> del b
>>> b=list()
>>> for ind,let in enumerate(a[0]):
b.append(let)
for ind,let in enumerate(a[1]):
b.append(let)
print(b)
SyntaxError: unindent does not match any outer indentation level
>>> for ind,let in enumerate(a[0]):
b.append(let)
for ind,let in enumerate(a[1]):
b.append(let)
print(b)
SyntaxError: unindent does not match any outer indentation level
>>> for ind,let in enumerate(a[0]):
b.append(let)
for ind,let in enumerate(a[1]):
b.append(let)
print(b)
['swallow', 'a', 'b', 'c']
['swallow', 'a', 'b', 'c', 'snake', 'a', 'b', 'c']
['swallow', 'a', 'b', 'c', 'snake', 'a', 'b', 'c', 'parrot', 'a', 'b', 'c']
>>> del b
>>> b=list()
>>> for ind,let in enumerate(a[1]):
print(let)
a
b
c
>>> for ind,let in enumerate(a[1]):
print(let[0])
a
b
c
>>> for ind,let in enumerate(a[1]):
print(let[1])
Traceback (most recent call last):
File "<pyshell#28>", line 2, in <module>
print(let[1])
IndexError: string index out of range
>>> a
(['swallow', 'snake', 'parrot'], 'abc')
>>> for ind, let in ennumerate(a):
print(ind,let)
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
for ind, let in ennumerate(a):
NameError: name 'ennumerate' is not defined
>>> for ind,let in enumerate(a):
print(ind,let)
0 ['swallow', 'snake', 'parrot']
1 abc
>>> for ind,let in enumerate(a[1]):
print(let)
a
b
c
>>> for ind,let in enumerate
SyntaxError: invalid syntax
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> c=['swallow', 'snake', 'parrot']
>>> lista
Traceback (most recent call last):
File "<pyshell#48>", line 1, in <module>
lista
NameError: name 'lista' is not defined
>>> b
[]
>>> for ind,let in enumerate(c):
b.append('{},{}'.format(ind,let))
print (b)
but i just don't know how to do it.
i need to see it done so i could understand it.
i try testing it before i put it in a function but it never works.
Kenneth Love
Treehouse Guest TeacherReynaldo Ponte You don't need to be going into index positions when you call enumerate()
. Use enumerate()
on a whole list.
def combo(list1, list2):
output = []
for index, value in enumerate(list1):
OK, so now I have index
, which is the index of list1
that I'm currently on. The first time, it'll be 0. The second time, it'll be 1. The third, 2. And so on.
I also have value
, which is what's in list1
at the current index
. So maybe it's 'swallow'
, like in the example. The second time, it'll be 'snake'
.
Now, I need to make a tuple which has the current value
, and whatever is in list2
at the current index
, and put that tuple into output
.
And, finally, I need to return output
.
Does that help?
Reynaldo Ponte
669 PointsReynaldo Ponte
669 Pointsthanks Kenneth.
I tried playing around with the hint you gave me and i got this.
I got a few different results as well with different combinations but i feel this code is the correct code i should be typing.
I know I am close. I am guessing I should try another for loop to get the words and the alphabets to become a tuple.
could you give me an other clue.
thanks for your help.