How can I go about swapping numbers in a given list?
For example:
list = [5,6,7,10,11,12]
I would like to swap 12
with 5
.
Is there an built-in Python function that can allow me to do that?
How can I go about swapping numbers in a given list?
For example:
list = [5,6,7,10,11,12]
I would like to swap 12
with 5
.
Is there an built-in Python function that can allow me to do that?
Use the index of the number you want to change.
Then you can make a quick swapping function
This is what finally worked for me.
You can use "*" operator.
Read here for more information.
Order of evaluation of the above expression:
First items on RHS are collected in a tuple, and then that tuple is unpacked and assigned to the items on the LHS.
you can swap using this code,
Another way (not so cute):