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:
Insert after every Nth element in a list - Python
Next article icon

Insert list in another list – Python

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

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

Using list.insert()

insert() method allows us to insert an element at a specific index in the list. By looping through the elements of the second list, we can insert them one by one into the first list at the desired position.

Python
a = [1, 2, 3, 4] b = [5, 6] index = 2  # Loop through each element in b and insert it into a at the specified index for item in b:     a.insert(index, item)  # Insert item from b at the specified index     index += 1  # Increment index to add next item after the previous one  print(a)  

Output
[1, 2, 5, 6, 3, 4] 

Explanation:

  • We use insert() to add each element from ‘b’ into ‘a’ at the specified index.
  • After each insertion, we increment the index to ensure elements from ‘b’ are added sequentially.

Let’s explore some more ways to insert on list in another list.

Table of Content

  • Using List Slicing
  • Using extend()
  • Using itertools.chain()
  • Using append() and reverse()

Using List Slicing

List Slicing method allows us to split the first list and insert the second list into the desired position.

Python
a = [1, 2, 3, 4] b = [5, 6] index = 2  # Slicing a before and after the index, then inserting b between a = a[:index] + b + a[index:]  print(a) 

Output
[1, 2, 5, 6, 3, 4] 

Explanation:

  • The list is sliced into two parts: before the specified index and after it.
  • We concatenate ‘b’ between these two slices, effectively inserting it into ‘a’.

Using extend()

extend() method can be used when we want to add all elements from the second list to the first list. This method is useful if we want to add the elements to the end or any specific index using slicing.

Python
a = [1, 2, 3, 4] b = [5, 6] index = 2  # Insert elements from b into a using slicing a[index:index] = b  print(a)  

Output
[1, 2, 5, 6, 3, 4] 

Explanation:

  • We use slicing to target the position where we want to insert ‘b’ and assign ‘b’ to that slice.
  • This method allows us to insert ‘b’ without shifting individual elements.

Using itertools.chain()

itertools.chain() function can be used to combine multiple iterables into a single one. By using chain(), we can merge both lists efficiently.

Python
import itertools  a = [1, 2, 3, 4] b = [5, 6] index = 2  # Use itertools.chain() to combine the three parts of the list a = list(itertools.chain(a[:index], b, a[index:]))  print(a)  

Output
[1, 2, 5, 6, 3, 4] 

Explanation: We use itertools.chain() to merge the parts of ‘a’ before and after the insertion index, along with the list ‘b’.

Using append() and reverse()

This method first appends all elements from ‘b’ to the end of ‘a’ and then reverses the lists for proper positioning.

Python
a = [1, 2, 3, 4] b = [5, 6] index = 2  # Extend a with b, then reverse the remaining part of a after the insert index a.extend(b) a[index + len(b):] = reversed(a[index + len(b):])  # Reverse the rest of the list  # Insert b into a at the specified index a[index:index] = b  print(a) 

Output
[1, 2, 5, 6, 3, 4, 6, 5] 

Explanation: We first extend ‘a’ with ‘b’, then we reverse the remaining portion of the list to keep the original order intact after inserting ‘b’.



Next Article
Insert after every Nth element in a list - Python
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Insert after every Nth element in a list - Python
    Inserting an element after every Nth item in a list is a useful way to adjust the structure of a list. For Example we have a list li=[1,2,3,4,5,6,7] Let's suppose we want to insert element 'x' after every 2 elements in the list so the list will look like li=[1,2,'x',3,4,'x',5,6,7] Iteration with ind
    4 min read
  • Move One List Element to Another List - Python
    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
    3 min read
  • Convert set into a list in Python
    In Python, sets are unordered collections of unique elements. While they're great for membership tests and eliminating duplicates, sometimes you may need to convert a set into a list to perform operations like indexing, slicing, or sorting. For example, if input set is {1, 2, 3, 4} then Output shoul
    3 min read
  • Python | Add list at beginning of list
    Sometimes, while working with Python list, we have a problem in which we need to add a complete list to another. The rear end addition to list has been discussed before. But sometimes, we need to perform an append at beginning of list. Let's discuss certain ways in which this task can be performed.
    5 min read
  • Python Initialize List of Lists
    A list of lists in Python is often used to represent multidimensional data such as rows and columns of a matrix. Initializing a list of lists can be done in several ways each suited to specific requirements such as fixed-size lists or dynamic lists. Let's explore the most efficient and commonly used
    3 min read
  • List As Input in Python in Single Line
    Python provides several ways to take a list as input in Python in a single line. Taking user input is a common task in Python programming, and when it comes to handling lists, there are several efficient ways to accomplish this in just a single line of code. In this article, we will explore four com
    3 min read
  • Print a List in Horizontally in Python
    Printing a list horizontally means displaying the elements of a list in a single line instead of each element being on a new line. In this article, we will explore some methods to print a list horizontally in Python. Using * (Unpacking Operator)unpacking operator (*) allows us to print list elements
    2 min read
  • Python | Find missing elements in List
    Sometimes, we can get elements in range as input but some values are missing in otherwise consecutive range. We might have a use case in which we need to get all the missing elements. Let's discuss certain ways in which this can be done. Method #1 : Using list comprehension We can perform the task o
    7 min read
  • Python | Convert list of tuples into list
    In Python we often need to convert a list of tuples into a flat list, especially when we work with datasets or nested structures. In this article, we will explore various methods to Convert a list of tuples into a list. Using itertools.chain() itertools.chain() is the most efficient way to flatten a
    3 min read
  • Python Program For Inserting A Node In A Linked List
    We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal.All programs discussed in this post consider the following representations of linked list. Python Code # Node class class Node: # Function to initialize the # n
    7 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