rem rem - 1 month ago 6
Python Question

Move an element in a list? | Python

So I wanted to for example, if a list has elements like

[A,B,C,.,D]


How would I get 'C' to move into the empty space to the right of it? I know I would first have to look and see if there is an period next to a character. What I am having trouble with is the moving part. I want to move 'C' and move the period in it's old spot.

Answer

You mean, like, changing C and the dot position? You could have something like:

list_ = [A, B, C, ., D]
tmp = list_[2]
list_[2] = list_[3]
list_[3] = tmp

The list_ would look like [A, B, ., C, D].

Comments