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:
Sort Tuple of Lists in Python
Next article icon

Python | Add tuple to front of list

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

Sometimes, while working with Python list, we can have a problem in which we need to add a new tuple to existing list. Append at rear is usually easier than addition at front. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using insert() 

This is one of the way in which the element can be added to front in one-liner. It is used to add any element in front of list. The behaviour is the same for tuple as well. 

Python3




# Python3 code to demonstrate working of
# Adding tuple to front of list
# using insert()
 
# Initializing list
test_list = [('is', 2), ('best', 3)]
 
# printing original list
print("The original list is : "
      + str(test_list))
 
# Initializing tuple to add
add_tuple = ('gfg', 1)
 
# Adding tuple to front of list
# using insert()
test_list.insert(0, add_tuple)
 
# printing result
print("The tuple after adding is : "
      + str(test_list))
 
 
Output
The original list is : [('is', 2), ('best', 3)] The tuple after adding is : [('gfg', 1), ('is', 2), ('best', 3)]

Time complexity: O(n) where n is the number of elements in the list as we are inserting an element in the front of the list which takes linear time.
Auxiliary Space: O(1) as we are not using any extra data structure and only inserting an element in the existing list.

Method #2 : Using deque() + appendleft() 

The combination of above functions can be used to perform this particular task. In this, we just need to convert the list into a deque so that we can perform the append at front using appendleft() 

Python3




# Python3 code to demonstrate working of
# Adding tuple to front of list
# using deque() + appendleft()
from collections import deque
 
# Initializing list
test_list = [('is', 2), ('best', 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing tuple to add
add_tuple = ('gfg', 1)
 
# Adding tuple to front of list
# using deque() + appendleft()
res = deque(test_list)
res.appendleft(add_tuple)
 
# printing result
print("The tuple after adding is : " + str(list(res)))
 
 
Output
The original list is : [('is', 2), ('best', 3)] The tuple after adding is : [('gfg', 1), ('is', 2), ('best', 3)]

Time complexity: O(1), where n is the length of the list.
Auxiliary space: O(1)

Method #3 : Using extend() method

Python3




# Python3 code to demonstrate working of
# Adding tuple to front of list
 
# Initializing list
test_list = [('is', 2), ('best', 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing tuple to add
add_tuple = ('gfg', 1)
 
# Adding tuple to front of list
x = [add_tuple]
x.extend(test_list)
# printing result
print("The tuple after adding is : " + str(x))
 
 
Output
The original list is : [('is', 2), ('best', 3)] The tuple after adding is : [('gfg', 1), ('is', 2), ('best', 3)]

Time Complexity: O(k), Where k is the length of the list that needs to be added.
Auxiliary Space: O(k)

Method #4: Using the concatenation operator
You can use the + operator to concatenate the list and the tuple, and it will add the tuple to the front of the list.

Python3




# Python3 code to demonstrate working of
# Adding tuple to front of list
# using concatenation operator
# Initializing list
test_list = [('is', 2), ('best', 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing tuple to add
add_tuple = ('gfg', 1)
 
# Adding tuple to front of list
test_list = [add_tuple] + test_list
 
# printing result
print("The tuple after adding is : " + str(test_list))
# This code is contributed by Edula Vinay Kumar Reddy
 
 
Output
The original list is : [('is', 2), ('best', 3)] The tuple after adding is : [('gfg', 1), ('is', 2), ('best', 3)]

Time Complexity: O(k), Where k is the length of the list that needs to be added.
Auxiliary Space: O(k)

Method #5 : Using slicing and unpacking

This method creates a new list that consists of the new tuple followed by the existing list, using the * operator to unpack the elements of the existing list.

Python3




# Python3 code to demonstrate working of
# Adding tuple to front of list
# using slicing and unpacking
# Initializing list
test_list = [('is', 2), ('best', 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing tuple to add
add_tuple = ('gfg', 1)
 
# Adding tuple to front of list
test_list = [add_tuple, *test_list]
 
# printing result
print("The tuple after adding is : " + str(test_list))
 
# This code is contributed by Edula Vinay Kumar Reddy
 
 
Output
The original list is : [('is', 2), ('best', 3)] The tuple after adding is : [('gfg', 1), ('is', 2), ('best', 3)]

Time complexity: O(n), where n is the length of the list, because creating a new list using slicing and unpacking requires iterating over all the elements of the existing list.
Auxiliary space: O(n+1), because it creates a new list that is one element longer than the original list. The additional element is the new tuple that is being added to the front of the list.

Method 6: Using list comprehension and the append() method

  1. Initialize the list test_list and print it.
  2. Initialize the tuple add_tuple and print it.
  3. Use a list comprehension to create a new list with the add_tuple at the beginning and the rest of the elements from test_list.
  4. Use the append() method to add each element from the new list to test_list.
  5. Print the updated list.

Python3




# Initializing list
test_list = [('is', 2), ('best', 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing tuple to add
add_tuple = ('gfg', 1)
 
# Adding tuple to front of list
test_list = [add_tuple] + [i for i in test_list]
 
# Printing result
print("The tuple after adding is : " + str(test_list))
 
# Alternative method
test_list = [('is', 2), ('best', 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# Initializing tuple to add
add_tuple = ('gfg', 1)
 
# Adding tuple to front of list
new_list = [add_tuple] + [i for i in test_list]
 
# Using append() method to add elements from new_list to test_list
for i in new_list:
    test_list.append(i)
 
# Printing result
print("The tuple after adding is : " + str(test_list))
 
 
Output
The original list is : [('is', 2), ('best', 3)] The tuple after adding is : [('gfg', 1), ('is', 2), ('best', 3)] The original list is : [('is', 2), ('best', 3)] The tuple after adding is : [('is', 2), ('best', 3), ('gfg', 1), ('is', 2), ('best', 3)]

Time complexity: O(n) for both methods as we need to create a new list and append elements to the existing list.
Auxiliary space: O(n) for the first method and O(2n) for the second method.

Method #7 : Using the unpacking operator (*) and the list() constructor

  1. Initialize the list “test_list” with the tuples
  2. Initialize the tuple “add_tuple” with the tuple to add to the front of the list
  3. Create a new list using the unpacking operator (*) to unpack the “add_tuple” and the original list “test_list”
  4. Use the list() constructor to create a new list from the unpacked elements
  5. Print the modified list.

Python3




# Initializing list
test_list = [('is', 2), ('best', 3)]
 
# Printing original list
print("The original list is : " + str(test_list))
 
# Initializing tuple to add
add_tuple = ('gfg', 1)
 
# Adding tuple to front of list
new_list = [add_tuple, *test_list]
test_list = list(new_list)
 
# Printing result
print("The tuple after adding is : " + str(test_list))
 
 
Output
The original list is : [('is', 2), ('best', 3)] The tuple after adding is : [('gfg', 1), ('is', 2), ('best', 3)]

Time complexity: O(n) (where n is the length of the list)
Auxiliary space: O(n)



Next Article
Sort Tuple of Lists in Python
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python - Add Custom Column to Tuple list
    Sometimes, while working with Python records, we can have a problem in which we need to add custom column to tuples list. This kind of problem can have application in data domains such as web development. Lets discuss certain ways in which this task can be performed. Input : test_list = [(3, ), (7,
    5 min read
  • Python - Add list elements to tuples list
    Sometimes, while working with Python tuples, we can have a problem in which we need to add all the elements of a particular list to all tuples of a list. This kind of problem can come in domains such as web development and day-day programming. Let's discuss certain ways in which this task can be don
    6 min read
  • How To Slice A List Of Tuples In Python?
    In Python, slicing a list of tuples allows you to extract specific subsets of data efficiently. Tuples, being immutable, offer a stable structure. Use slicing notation to select ranges or steps within the list of tuples. This technique is particularly handy when dealing with datasets or organizing i
    3 min read
  • Sort Tuple of Lists in Python
    The task of sorting a tuple of lists involves iterating through each list inside the tuple and sorting its elements. Since tuples are immutable, we cannot modify them directly, so we must create a new tuple containing the sorted lists. For example, given a tuple of lists a = ([2, 1, 5], [1, 5, 7], [
    3 min read
  • Python - Group list of tuples to dictionary
    The task is to convert a list of tuples into a dictionary, where each tuple consists of two elements. The first element of each tuple becomes the key and the second element becomes the value. If a key appears multiple times, its corresponding values should be grouped together, typically in a list. F
    4 min read
  • Convert List to Tuple in Python
    The task of converting a list to a tuple in Python involves transforming a mutable data structure list into an immutable one tuple. Using tuple()The most straightforward and efficient method to convert a list into a tuple is by using the built-in tuple(). This method directly takes any iterable like
    2 min read
  • Python - Add Values to Dictionary of List
    A dictionary of lists allows storing grouped values under specific keys. For example, in a = {'x': [10, 20]}, the key 'x' maps to the list [10, 20]. To add values like 30 to this list, we use efficient methods to update the dictionary dynamically. Let’s look at some commonly used methods to efficien
    3 min read
  • Python | Summation of tuples in list
    Sometimes, while working with records, we can have a problem in which we need to find the cumulative sum of all the values that are present in tuples. This can have applications in cases in which we deal with a lot of record data. Let's discuss certain ways in which this problem can be solved. Metho
    7 min read
  • Print a List of Tuples in Python
    The task of printing a list of tuples in Python involves displaying the elements of a list where each item is a tuple. A tuple is an ordered collection of elements enclosed in parentheses ( ), while a list is an ordered collection enclosed in square brackets [ ]. Using print()print() function is the
    2 min read
  • How to Take List of Tuples as Input in Python?
    Lists of tuples are useful data structures in Python and commonly used when you need to group related elements together while maintaining immutability within each group. We may need to take input for a list of tuples from the user. This article will explore different methods to take a list of tuples
    3 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