The 365 Data Science team is proud to invite you to our own community forum. A very well built system to support your queries, questions and give the chance to show your knowledge and help others in their path of becoming Data Science specialists.
Ask
Anybody can ask a question
Answer
Anybody can answer
Vote
The best answers are voted up and moderated by our team

Nested for loop question

Nested for loop question

1
Vote
1
Answer
  • 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?

Screenshot

1 Answer

2
Votes

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…
 
 
 

Thank you!

5 months