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 list in another list - Python
Next article icon

Python | Add list at beginning of list

Last Updated : 02 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Method #1 : Using "+" operator The "+" operator can be used to perform this particular task. In this, we just perform the addition of one list before other and construct a new list or perform the addition to same list. 

Python3
# Python3 code to demonstrate working of # Adding list at beginning of list  # initialize list test_list = [1, 4, 5, 7, 6]  # initialize add list add_list = [3, 4, 2, 10]  # printing original list print("The original list is : " + str(test_list))  # printing add list  print("The add list is : " + str(add_list))  # Adding list at beginning of list test_list = add_list + test_list  # printing result print("The original updated list is : " + str(test_list)) 

Output
The original list is : [1, 4, 5, 7, 6] The add list is : [3, 4, 2, 10] The original updated list is : [3, 4, 2, 10, 1, 4, 5, 7, 6]

Time Complexity: O(n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the add_list 

  Method #2 : Using deque.extendleft() + reversed() This task can also be performed using combination of above methods. In this, we just convert the list into a dequeue, to allow a front append, and then one by one addition is done by extendleft(), the add list is reversed so that addition take place in correct order using reversed(). 

Python3
# Python3 code to demonstrate working of # Adding list at beginning of list # using deque.extendleft() + reversed() from collections import deque  # initialize list test_list = [1, 4, 5, 7, 6]  # initialize add list add_list = [3, 4, 2, 10]  # printing original list print("The original list is : " + str(test_list))  # printing add list  print("The add list is : " + str(add_list))  # Adding list at beginning of list # using deque.extendleft() + reversed() res = deque(test_list) res.extendleft(reversed(add_list))  # printing result print("The original updated list is : " + str(list(res))) 

Output
The original list is : [1, 4, 5, 7, 6] The add list is : [3, 4, 2, 10] The original updated list is : [3, 4, 2, 10, 1, 4, 5, 7, 6]

Time Complexity: O(n*n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list 

Method #3 : Using extend() method

Python3
# Python3 code to demonstrate working of # Adding list at beginning of list # using "+" operator  # initialize list test_list = [1, 4, 5, 7, 6]  # initialize add list add_list = [3, 4, 2, 10]  # printing original list print("The original list is : " + str(test_list))  # printing add list print("The add list is : " + str(add_list))  add_list.extend(test_list) test_list=add_list # printing result print("The original updated list is : " + str(test_list)) 

Output
The original list is : [1, 4, 5, 7, 6] The add list is : [3, 4, 2, 10] The original updated list is : [3, 4, 2, 10, 1, 4, 5, 7, 6]

Time Complexity: O(n*n), where n is the length of the list test_list 
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the list 

Method #4 : Using append() method

Approach 

  1. Initiate a for loop to traverse test_list
  2. Append each element to add_list
  3. Display add_list
Python3
# Python3 code to demonstrate working of # Adding list at beginning of list # using "+" operator  # initialize list test_list = [1, 4, 5, 7, 6]  # initialize add list add_list = [3, 4, 2, 10]  # printing original list print("The original list is : " + str(test_list))  # printing add list print("The add list is : " + str(add_list))  for i in test_list:     add_list.append(i) # printing result print("The original updated list is : " + str(add_list)) 

Output
The original list is : [1, 4, 5, 7, 6] The add list is : [3, 4, 2, 10] The original updated list is : [3, 4, 2, 10, 1, 4, 5, 7, 6]

Method #5 : Using insert() method

Approach

  • Initiate a for loop to traverse test_list
  • Insert each element to add_list using index
  • Display add_list
Python
# Python3 code to demonstrate working of # Adding list at beginning of list # using insert  # initialize list test_list = [1, 4, 5, 7, 6]  # initialize add list add_list = [3, 4, 2, 10]  # printing original list print("The original list is : " + str(test_list))  # printing add list print("The add list is : " + str(add_list))  for i,j in enumerate(test_list):     add_list.insert(i,j)      # printing result print("The original updated list is : " + str(add_list)) 

Output
The original list is : [1, 4, 5, 7, 6] The add list is : [3, 4, 2, 10] The original updated list is : [1, 4, 5, 7, 6, 3, 4, 2, 10] 

Time Complexity: O(N) N length of the list

Auxiliary Space: O(M) M length of add_list


Next Article
Insert list in another list - Python
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Flatten a List of Lists in Python
    Flattening a list of lists means turning a nested list structure into a single flat list. This can be useful when we need to process or analyze the data in a simpler format. In this article, we will explore various approaches to Flatten a list of Lists in Python. Using itertools.chain itertools modu
    3 min read
  • Python - Append items at beginning of dictionary
    The task of appending items at the beginning of a dictionary in Python involves inserting new key-value pairs at the start of an existing dictionary, rather than adding them at the end. Since dictionaries in Python preserve insertion order, this operation allows us to effectively reorder the diction
    3 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 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
  • Iterate Over a List of Lists in Python
    We are given a list that contains multiple sublists, and our task is to iterate over each of these sublists and access their elements. For example, if we have a list like this: [[1, 2], [3, 4], [5, 6]], then we need to loop through each sublist and access elements like 1, 2, 3, and so on. Using Nest
    2 min read
  • Add Elements of Two Lists in Python
    Adding corresponding elements of two lists can be useful in various situations such as processing sensor data, combining multiple sets of results, or performing element-wise operations in scientific computing. List Comprehension allows us to perform the addition in one line of code. It provides us a
    3 min read
  • Python - Convert a list into tuple of lists
    When working with data structures in Python, there are times when we need to convert a list into a tuple of smaller lists. For example, given a list [1, 2, 3, 4, 5, 6], we may want to split it into a tuple of two lists like ([1, 2, 3], [4, 5, 6]). We will explore different methods to achieve this co
    3 min read
  • Python - Add List Items
    Python lists are dynamic, which means we can add items to them anytime. In this guide, we'll look at some common ways to add single or multiple items to a list using built-in methods and operators with simple examples: Add a Single Item Using append()append() method adds one item to the end of the l
    3 min read
  • Sort List of Lists Ascending and then Descending in Python
    Sorting a list of lists in Python can be done in many ways, we will explore the methods to achieve the same in this article. Using sorted() with a keysorted() function is a flexible and efficient way to sort lists. It creates a new list while leaving the original unchanged. [GFGTABS] Python a = [[1,
    3 min read
  • How to Find Index of Item in Python List
    To find the index of given list item in Python, we have multiple methods depending on specific use case. Whether we’re checking for membership, updating an item or extracting information, knowing how to get an index is fundamental. Using index() method is the simplest method to find index of list it
    2 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