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 (Retired) Tuples Combo

Gregory Heard
Gregory Heard
1,975 Points

Why isn't this working? It returns correctly locally.

This works. Why is the code challenge not completing?

zippy.py
# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]
# If you use .append(), you'll want to pass it a tuple of new values.

def combo (a, b):
    c = []
    for i in a:
        c.append((i, b[i-1]))
    return(c)

6 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Jeremy is correct. The challenge explictly asks for a list to be returned. Not an array, which is what you're currently returning. But even if we fix that, it still doesn't work. And that's because you need for i in a range. I rewrote your solution. Hope this helps!

def combo(a, b):
  c = list()
  for i in range(len(a)):
    c.append((a[i], b[i]))
  return c
Gregory Heard
Gregory Heard
1,975 Points

Hi Jennifer,

Thanks a lot!

Only one thing though, i thought you initiliase an empty list with [] ? What's the difference between that and list(). Also what does the range(len()) do?

Appreciate the help!

Even though the code may work on your machine it may not be the output that the challenge is looking for or the way that you coded it. They usually check for consistency with the way that they are teaching you.

Gregory Heard
Gregory Heard
1,975 Points

I have gone through it with an experienced programmer i work with, and he can't understand why it isn't working either. That's the thing.

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

It says your string index is out of range. Even if it works locally, doesnt mean it works for the challenge. If your code isnt what the challenge is looking for, it will be wrong.

Gregory Heard
Gregory Heard
1,975 Points

OK. Could I have some suggestions on what it could be? I am completing it to the specification that is being described.

Thanks!

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

Here is 1 way to do it (it passes the challenge:

my_list = [] def combo(list, str): #x = 0 for idx, value in enumerate(str): b = value a = list[idx] c = a, b my_list.append(c) #x = x + 1 continue #print(my_list)

return my_list