- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Move an element to the end of a list in Python?
In this article, the user will learn how to move an element to the end of a list in Python. Out of the different manipulation techniques, the various in-built method is available in the Python language to move an element in the specific index, front, or at the end of a list. Manipulating lists is an essential aspect of working with Python programming language, and understanding how these different methods work provides flexibility when required. The two most common methods are using pop() and append(), or remove() and insert().
Multiple Approaches
Approach 1 − Using pop() and append() method
Approach 2 − Using the del() and concatenation method
Approach 3 − Using remove() and insert() method
Approach 1: By using pop() and append() methods
The first method involves using the pop() function and append() method available in Python. The pop() function removes the specified index from the list and returns its value. Simultaneously, the ‘append’ method adds elements to a list.
Algorithm
The list is named “list1” with 5 elements: 1, 2, ‘four’, 4, and 5.
Then, remove the element at index 2 (‘four’) from list1 using the pop() method and store it in a variable called move_element
Append the value of move_element to the end of list1 using the append() method.
The Print function returns the contents of list1
Example
#initializing the list with integer and string elements list1 = [1,2,'four',4,5] #move_element variable is declared with the pop() function #Using the pop method to remove the element from the index value of 2 move_element = list1.pop(2) #append() method to add the elements list1.append(move_element) #finally the modified list is printed print("The list after moving the element is:",list1)
Output
The list after moving the element is: [1, 2, 4, 5, 'four']
Approach 2: By using the del() and concatenation methods
The elements in the list are moved to the end of the list using the del and “+” operator.
Algorithm
Initialize the list and the element to be moved.
Find the index of the element using the index() method
Eliminate the element from the list using the del statement.
Use the append() method, to append the element at the rear of the list.
Then finally print the modified list.
Example
#initializing the input values list1 = [1, 2, 'four', 4, 5] #initializing the variable that needs to be moved to the end of the list move_element = 'four' #using the del function del list1[list1.index(move_element)] #new list is created which adds the list after removing the element and the removed element list1 = list1 + [move_element] #print statement returns the final list print("The list after moving the element is:",list1)
Output
The list after moving the element is: [1, 2, 4, 5, 'four']
Approach 3: By using remove() and insert() methods
Another approach is done either through removing specific values or delimiters needing replacements within functions such as remove(). Insertions are performed via another handy and less tricky feature known as insert( ) method.
Algorithm
Create a list called list1 with 5 elements: 1, 2, ‘four’, 4, and 5.
Store the value ‘four’ in a variable called move_element.
Remove the first occurrence of move_element from list1 using the remove() method.
Insert the value of move_element at the end of list1 using the insert() method.
Then Print the contents of list1
Example
#Intializing the list with the elements list1 = [1, 2, 'four', 4, 5] #the element which has to be moved to the end is declared move_element = 'four' #the element removed from the list using remove() method list1.remove(move_element) #using the insert method list1.insert(len(list1), move_element) #finally, the modified list is printed print("The list after moving the element is:",list1)
Output
The list after moving the element is: [1, 2, 4, 5, 'four']
Conclusion
The list is generally composed of various elements and is identified by the index value. The index value starts from the “0” value and continues up to the number of lists present. The elements present in the list can be moved to the end of the list using various approaches like remove, del, insert, append, and concatenation methods.