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 (2016, retired 2019) Tuples Combo

Anton Sorokin
seal-mask
.a{fill-rule:evenodd;}techdegree
Anton Sorokin
Python Web Development Techdegree Student 2,173 Points

Can you suggest more elegant solution?

I would like to share the following code (tuples -> combo). Code is working and passed, but you may know more elegant solution.

# combo([1, 2, 3], 'abc')
# Output:
# [(1, 'a'), (2, 'b'), (3, 'c')]

def combo(*some_iterables):
    new_tuple = *some_iterables[0], *some_iterables[1]  # create new tuple

    l = int(len(new_tuple)/2)  # half of new_tuple length, int
    sorted_tupl= ()
    new_list = [] 
    for i in range(l):
        sorted_tupl = new_tuple[i], new_tuple[i-l]  # creates list of sorted tuples
        new_list.append((sorted_tupl))

    return(new_list)

2 Answers

Steven Parker
Steven Parker
229,787 Points

The simplest and most elegant solution would be to use the zip function.

You're not allowed to use this on the challenge, but the combo function itself can be constructed with one simple expression:

def combo(one, two):
    return list(zip(one, two))
Sherwin Shann
Sherwin Shann
3,206 Points

Hi Anton,

You can do this as well:

def combo(*some_iterables):  #assume lengh of each iterables are the same
    new_list = []
    for i in range(len(some_iterables[0])):
        return_tuple = some_iterables[0][i], some_iterables[1][i]
        new_list.append(return_tuple)
    return(new_list)