Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Operation to each element in list
Next article icon

Move One List Element to Another List – Python

Last Updated : 10 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The task of moving one list element to another in Python involves locating a specific element in the source list, removing it, and inserting it into the target list at a desired position. For example, if a = [4, 5, 6, 7, 3, 8] and b = [7, 6, 3, 8, 10, 12], moving 10 from b to index 4 in a results in a = [4, 5, 6, 7, 10, 3, 8] and b = [7, 6, 3, 8, 12].

Using list comprehension

This method efficiently moves an element from one list to another in a single step using list comprehension. It minimizes function calls and optimizes performance by finding and inserting the element in a compact and readable way. This approach is ideal for quick modifications in small to medium-sized lists.

Python
a = [4, 5, 6, 7, 3, 8] b = [7, 6, 3, 8, 10, 12]  [a.insert(4, b.pop(idx)) for idx in [b.index(10)]]  print(a)  # After insert print(b)  # After removal 

Output
[4, 5, 6, 7, 10, 3, 8] [7, 6, 3, 8, 12] 

Explanation: b.pop(idx) removes the element 10 from list b and a.insert(4, b.pop(idx)) inserts this removed element at index 4 in list a, effectively moving 10 from b to a.

Table of Content

  • Using pop() and insert()
  • Using filter()
  • Using loop

Using pop() and insert()

A direct method where pop is used to remove an element from one list and insert places it into another. This approach ensures clarity while maintaining efficiency. It is widely used for moving elements between lists without unnecessary complexity.

Python
a = [4, 5, 6, 7, 3, 8] b = [7, 6, 3, 8, 10, 12]  idx = b.index(10) val = b.pop(idx) a.insert(4, val)  print(a) print(b) 

Output
[4, 5, 6, 7, 10, 3, 8] [7, 6, 3, 8, 12] 

Explanation: This code first finds the index of the element 10 in list b using b.index(10), storing it in idx. Then, b.pop(idx) removes 10 from b and stores it in val. Finally, a.insert(4, val) inserts 10 at index 4 in list a, effectively moving the element from b to a.

Using filter()

filter() method quickly finds the element’s index before removing and inserting it into the target list. By avoiding unnecessary iterations, it enhances efficiency, making it a good choice for handling large lists where performance is a concern.

Python
a = [4, 5, 6, 7, 3, 8] b = [7, 6, 3, 8, 10, 12]  idx = next(filter(lambda i: b[i] == 10, range(len(b)))) a.insert(4, b.pop(idx))  print(a) print(b) 

Output
[4, 5, 6, 7, 10, 3, 8] [7, 6, 3, 8, 12] 

Explanation: filter() iterates over b’s indices, finding where b[i] == 10 and next retrieves the first matching index as idx. Then, b.pop(idx) removes 10 from b and a.insert(4, b.pop(idx)) places it at index 4 in a, completing the transfer.

Using loop

A simple but less efficient method where a loop finds the element, removes it and inserts it into another list. While not the fastest, it offers flexibility for handling custom conditions when moving elements between lists.

Python
a = [4, 5, 6, 7, 3, 8] b = [7, 6, 3, 8, 10, 12]  for i in range(len(b)):     if b[i] == 10:         a.insert(4, b.pop(i))         break  print(a) print(b) 

Output
[4, 5, 6, 7, 10, 3, 8] [7, 6, 3, 8, 12] 

Explanation: loop iterates through b, finds 10, removes it using b.pop(i) and inserts it at index 4 in a. The break ensures only the first occurrence is moved efficiently.



Next Article
Python - Operation to each element in list
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python - Operation to each element in list
    Given a list, there are often when performing a specific operation on each element is necessary. While using loops is a straightforward approach, Python provides several concise and efficient methods to achieve this. In this article, we will explore different operations for each element in the list.
    3 min read
  • Python - Move Element to End of the List
    We are given a list we need to move particular element to end to the list. For example, we are given a list a=[1,2,3,4,5] we need to move element 3 at the end of the list so that output list becomes [1, 2, 4, 5, 3]. Using remove() and append()We can remove the element from its current position and t
    3 min read
  • Python | Move given element to List Start
    The conventional problem involving the element shifts has been discussed many times earlier, but sometimes we have strict constraints performing them, and knowledge of any possible variation helps. This article talks about one such problem of shifting K’s at the start of the list, catch here is it c
    6 min read
  • Insert list in another list - Python
    We are given two lists, and our task is to insert the elements of the second list into the first list at a specific position. For example, given the lists a = [1, 2, 3, 4] and b = [5, 6], we want to insert list 'b' into 'a' at a certain position, resulting in the combined list a = [1, 2, 5, 6, 3, 4]
    4 min read
  • Python - Merge list elements
    Merging list elements is a common task in Python. Each method has its own strengths and the choice of method depends on the complexity of the task. This is the simplest and most straightforward method to merge elements. We can slice the list and use string concatenation to combine elements. [GFGTABS
    2 min read
  • Python | List Element Count with Order
    Sometimes, while working with lists or numbers we can have a problem in which we need to attach with each element of list, a number, which is the position of that element's occurrence in that list. This type of problem can come across many domains. Let's discuss a way in which this problem can be so
    4 min read
  • Python | Assign range of elements to List
    Assigning elements to list is a common problem and many varieties of it have been discussed in the previous articles. This particular article discusses the insertion of range of elements in the list. Let's discuss certain ways in which this problem can be solved. Method #1 : Using extend() This can
    5 min read
  • Python - Sort list according to other list order
    Sorting a list based on the order defined by another list is a common requirement in Python, especially in scenarios involving custom sorting logic. This ensures that elements in the first list appear in the exact order specified in the second list. Python provides various methods to accomplish this
    2 min read
  • Python | Convert list of tuples to list of list
    Converting list of tuples to list of lists in Python is a task where each tuple is transformed into list while preserving its elements. This operation is commonly used when we need to modify or work with the data in list format instead of tuples. Using numpyNumPy makes it easy to convert a list of t
    3 min read
  • Python | Add list elements with a multi-list based on index
    Given two lists, one is a simple list and second is a multi-list, the task is to add both lists based on index. Example: Input: List = [1, 2, 3, 4, 5, 6] List of list = [[0], [0, 1, 2], [0, 1], [0, 1], [0, 1, 2], [0]] Output: [[1], [2, 3, 4], [3, 4], [4, 5], [5, 6, 7], [6]] Explanation: [1] = [1+0]
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences