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

Chris Komaroff
PLUS
Chris Komaroff
Courses Plus Student 14,198 Points

Is teacher's answer for Tuples with Functions challenge 1 of 1 correct? "Bummer expected (10,T)" is wrong?

Challenge 1 of 1 code comment:

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

That's what my function combo() gives. I tested in workspace. Not following this.

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( iter1, iter2 ):
  return_list = []
  temp_dict = {}
  list_len = len( iter1 )
  i = 0
  while i < list_len:
    temp_dict[ iter1[i] ] = iter2[i]
    i += 1
  for key, value in temp_dict.items():
    return_list.append( (key,value) )
  return return_list

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

The ordering of items found when iterating on a dictionary can not be guaranteed. By constructing a temp_dict then reading key/value pairs out, you are scrambling the tuple order. Instead you can build add append the tuples directly

def combo( iter1, iter2 ):
    return_list = []
    list_len = len( iter1 )
    i = 0
    while i < list_len:
        return_list.append((iter1[i], iter2[i]))
        i += 1
    return return_list
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

There are many ways to improve on this code:

def combo( iter1, iter2 ):
    return_list = []
    for idx, item in enumerate(iter1):
        return_list.append((item, iter2[idx]))
    return return_list

Or....

def combo( iter1, iter2 ):
    return list(zip(iter1, iter2))