So I have a list containing a number of elements, each of which are sublists containing two elements; a character and a number. Example:
unordered_list = [["A", 5], ["B", 3], ["C", 10], ["D", 0]]
I have a bubble sort function that orders the sublists in descending order, according to the value in index 1 of each sublist.
def list_sort(n):
ordered = False
passes = 0
while not ordered:
passes += 1
ordered = True
for i in range(0, len(n)-passes):
if n[i][1] < n[i+1][1]:
n[i], n[i+1] = n[i+1], n[i]
ordered = False
return n
The function works just fine, it's just that if I pass my unordered_list variable into the function in an assignment, ie;
sorted_list = list_sort(unordered_list)
sorted_list --> [["C", 10], ["A", 5], ["B", 3], ["D", 0]]
The sorted_list contains the results I'd expect, however the unsorted_list passed into the function is also ordered after the function call, which is not what I'd expect, as surely only the local variable n in the function and the sorted_list variable should be changed?
What's going on here?