Python – Move item to the end of the list

Python Methods and Functions

List manipulation is quite common in daytime scheduling. You may encounter various problems where you want to run using only one line. One of these problems can be to move an element of the list to the bottom (end of the list). Let's discuss some ways this can be done.

Method #1 : Using sort() + key = (__eq__)

The sorting method can also be used to solve this particular problem, in which we provide a key equal to the row we want to shift so that it is moved to the end.

# Python3 code to demonstrate
# moving element to end
# using sort() + key = (__eq__)

# initializing list
test_list = ['3', '5', '7', '9', '11']

# printing original list
print ("The original list is : " + str(test_list))

# using sort() + key = (__eq__)
# moving element to end
test_list.sort(key = '5'.__eq__)

# printing result
print ("The modified element moved list is : " + str(test_list))

Output:

The original list is : ['3', '5', '7', '9', '11']
The modified element moved list is : ['3', '7', '9', '11', '5']

Method No. 2: using append () + pop () + index ()

This particular functionality can be performed in one line by combining these functions. The append function adds the element removed from the pop function using the index provided by the index function.

# Python3 code to demonstrate
# moving element to end
# using append() + pop() + index()

# initializing list
test_list = ['3', '5', '7', '9', '11']

# printing original list
print ("The original list is : " + str(test_list))

# using append() + pop() + index()
# moving element to end
test_list.append(test_list.pop(test_list.index(5)))

# printing result
print ("The modified element moved list is : " + str(test_list))

Output:

The original list is : ['3', '5', '7', '9', '11']
The modified element moved list is : ['3', '7', '9', '11', '5']




How do I move an element in my list to the end in Python?

StackOverflow question

I have a list of strings called values and I want to make an element in the list to be the very last element. For example, if I have the string:

['string1', 'string2', 'string3']

I want string2 to be the very last element:

['string1', 'string3', 'string2']

There also may be an instance when my list does not contain string2. Is there an easy way to do this? This is what I have so far:

if 'string2' in values:
    for i in values:
        #remove string2 and append to end
MOVE ITEM TO THE END OF THE LIST

Answer

>>> lst = ['string1', 'string2', 'string3']
>>> lst.append(lst.pop(lst.index('string2')))
>>> lst
['string1', 'string3', 'string2']

We look for the index of 'string2', pop that index out of the list and then append it to the list.

Perhaps a somewhat more exception free way is to add the thing you're looking for to the end of the list first (after all, you already presumably know what it is). Then delete the first instance of that string from the list:

>>> lst = ['string1', 'string2', 'string3']
>>> lst.append('string2')
>>> del lst[lst.index('string2')]  # Equivalent to lst.remove('string2')
>>> lst
['string1', 'string3', 'string2']




Tutorials