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

QuickSort Function Question

Hi, here's my code, my question is in the comment below:

def partition(l):
    length = len(l)
    pivot = l[length-1]
    left = []
    right = []

    for i in range(length-1):
        element = l[i]
        if element <= pivot:
            left+=[element]
        else: 
            right +=[element]
    return (left, pivot, right)

print partition([1, 5, 7, 3])

def quick_sort(list):
    #base case
    if len(list) <= 1:
        return list
    # recursive case
    else:
        (left, pivot, right) = partition(list) 
#whats this line above doing exactly? importing the inputs from the other function?
        sorted_left = quick_sort(left)
        sorted_right = quick_sort(right)
        return sorted_left + [pivot] + sorted_right

print quick_sort([1, 5, 7, 3])

Kenneth Love

2 Answers

Yes, you are correct! You're taking the output of the function 'partition' (which is a tuple) and saving it into a tuple that is local to the function 'quick_sort.'

Fareez,

Trevor is correct. I just want to note that "list" in Python is a builtin function. It is also a reserved keyword as you can see by the red color in the editor. The use of 'list' as a variable name can be confusing for the person following you trying to enhance or debug your code.

Ron

Oh how true Ron!