Change language

Python – Move item to the end of the list

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

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically