Resolved: Nested for loop question
- Course: The Python Programmer Bootcamp
- Section: For loops
- Lesson: The power of lists and loops and working together
- Question: I'm having trouble understanding the code in the example. Could someone give me a breakdown ow what is going on in each line?
1 answers ( 0 marked as helpful)
data_copy=data[:] #here you copy list data to data_copy
for i in range(len(data_copy)): #starting 1st loop
for j in range(0,len(data_copy)-i-1): #starting 2nd loop
if data_copy[j] > data_copy[j+1]: # conditions for bubble sorting
data_copy[j],data_copy[j+1]=data_copy[j+1],data_copy[j] #swapping elements
hope it helps...