Skip to content
Advertisement

Why doesn’t parallel swapping work with variables in python?

JavaScript

But, when you assign them to a variable you get a different solution.

JavaScript

My best guess is that it has something to do with were the variables are pointing to in memory.

Advertisement

Answer

I think the list is confusing you, so here is an equivalent example without a list:

JavaScript

vs

JavaScript

As I hope you can clearly see from the second piece of code, here we are have the values 1 and 2 in a and b respectively, and we are assigning them to c and d, and then swapping c and d. No where did we swap between a and b, so it remains a = 1 and b = 2. Only c and d were swapped.

Advertisement