rem rem - 1 year ago 106
Python Question

Move an element in a list? | Python

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


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 Source

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].

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download