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 | Add tuple to front of list
Next article icon

Python – Add Custom Column to Tuple list

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

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, ), (2, )] cus_eles = [7, 8, 2] 
Output : [(3, 7), (7, 8), (2, 2)] 

Input : test_list = [(3, 9, 6, 10)] cus_eles = [7] 
Output : [(3, 9, 6, 10, 7)]

Method #1: Using list comprehension + zip() The combination of above functionalities can be used to solve this problem. In this, we perform the pairing of custom elements and tuples with the help of zip(). 

Python3




# Python3 code to demonstrate working of
# Add Custom Column to Tuple list
# Using list comprehension + zip()
 
# initializing list
test_list = [(3, 4), (78, 76), (2, 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing add list
cus_eles = [17, 23, 12]
 
# Add Custom Column to Tuple list
# Using list comprehension + zip()
res = [sub + (val, ) for sub, val in zip(test_list, cus_eles)]
 
# printing result
print("The tuples after adding elements : " + str(res))
 
 
Output : 
The original list is : [(3, 4), (78, 76), (2, 3)] The tuples after adding elements : [(3, 4, 17), (78, 76, 23), (2, 3, 12)]

Time complexity: O(n),
Auxiliary space: O(n)

Method #2: Using map() + lambda The combination of above functions can also be used to solve this problem. In this, we perform the task of extending logic to each tuple using map() and lambda is used to perform task of addition. 

Python3




# Python3 code to demonstrate working of
# Add Custom Column to Tuple list
# Using map() + lambda
 
# initializing list
test_list = [(3, 4), (78, 76), (2, 3)]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing add list
cus_eles = [17, 23, 12]
 
# Add Custom Column to Tuple list
# Using map() + lambda
res = list(map(lambda a, b: a +(b, ), test_list, cur_eles))
 
# printing result
print("The tuples after adding elements : " + str(res))
 
 
Output : 
The original list is : [(3, 4), (78, 76), (2, 3)] The tuples after adding elements : [(3, 4, 17), (78, 76, 23), (2, 3, 12)]

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

Method #3: Add Custom Column to Tuple List using List Comprehension and Zip Function.

Use list comprehension and the built-in zip() function to iterate over both lists in parallel. For each pair of elements (a, b) where a is a tuple from test_list and b is an element from cus_eles, the program creates a new tuple (a[0], a[1], b) where a[0] and a[1] represent the first and second elements of a, respectively, and b is the custom element from cus_eles.

Python3




test_list = [(3, 4), (78, 76), (2, 3)]
cus_eles = [17, 23, 12]
 
res = [(a[0], a[1], b) for a, b in zip(test_list, cus_eles)]
print("The tuples after adding elements : ", res)
 
 
Output
The tuples after adding elements :  [(3, 4, 17), (78, 76, 23), (2, 3, 12)]

Time complexity: O(n), where n is the length of the input list,
Auxiliary space: O(n), as we are creating a new list of the same size as the input list.

Method #4: Using a for loop

Iterates through the index of test_list and concatenates each tuple with the corresponding element in cur_eles. The result is a list of tuples with the added elements.

Python3




test_list = [(3, 4), (78, 76), (2, 3)]
cur_eles = [17, 23, 12]
 
res = []
for i in range(len(test_list)):
    res.append(test_list[i] + (cur_eles[i],))
 
print("The tuples after adding elements : " + str(res))
 
 
Output
The tuples after adding elements : [(3, 4, 17), (78, 76, 23), (2, 3, 12)]

Time complexity: O(n), where n is the length of the input list test_list. 
Auxiliary space: O(n), since the output list res has n elements, and the input lists test_list and cur_eles also have n elements.

Method #5: using the pandas library.

Python3




# Python3 code to demonstrate working of
# Add Custom Column to Tuple list
# Using pandas library
 
import pandas as pd
 
# initializing list
test_list = [(3, 4), (78, 76), (2, 3)]
 
# converting list of tuples to a pandas dataframe
df = pd.DataFrame(test_list, columns=['col1', 'col2'])
 
# initializing add list
cus_eles = [17, 23, 12]
 
# adding a new column to the dataframe
df['new_col'] = cus_eles
 
# converting the dataframe back to a list of tuples
res = [tuple(x) for x in df.to_numpy()]
 
# printing result
print("The tuples after adding elements : " + str(res))
 
 

Output:

The tuples after adding elements : [(3, 4, 17), (78, 76, 23), (2, 3, 12)]

Time complexity: O(n), where n is the number of tuples in the list. 
Auxiliary space: O(n), where n is the number of tuples in the list. 



Next Article
Python | Add tuple to front of list
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • 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
  • Python | Convert Lists to column tuples
    Sometimes, while working with data, we can have a problem in which we need to get alike index elements in a single container. This means the columns of Matrix/Multiple lists need to be converted to list of tuples, each comprising of like index elements. Let's discuss certain ways in which this task
    11 min read
  • Python | Add tuple to front of list
    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 t
    7 min read
  • Python - Add K to Minimum element in Column Tuple List
    Sometimes, while working with Tuple records, we can have a problem in which we need to perform task of adding certain element to max/ min element to each column of Tuple list. This kind of problem can have application in web development domain. Let's discuss a certain way in which this task can be p
    8 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 - Convert Matrix to Custom Tuple Matrix
    Sometimes, while working with Python Matrix, we can have a problem in which we need to perform conversion of a Python Matrix to matrix of tuples which a value attached row-wise custom from external list. This kind of problem can have applications in data domains as Matrix is integral DS that is used
    6 min read
  • Python | Summation of Kth Column of Tuple List
    Sometimes, while working with Python list, we can have a task in which we need to work with tuple list and get the possible accumulation of its Kth index. This problem has applications in the web development domain while working with data information. Let's discuss certain ways in which this task ca
    7 min read
  • Python | Column Mean in tuple list
    Sometimes, while working with records, we can have a problem in which we need to average all the columns of a container of lists which are tuples. This kind of application is common in web development domain. Let’s discuss certain ways in which this task can be performed. Method #1 : Using sum() + l
    4 min read
  • Create a tuple from string and list - Python
    The task of creating a tuple from a string and a list in Python involves combining elements from both data types into a single tuple. The list elements are added as individual items and the string is treated as a single element within the tuple. For example, given a = ["gfg", "is"] and b = "best", t
    3 min read
  • Python | Adding N to Kth tuple element
    Many times, while working with records, we can have a problem in which we need to change the value of tuple elements. This is a common problem while working with tuples. Let's discuss certain ways in which N can be added to Kth element of tuple in list. Method #1 : Using loop Using loops this task c
    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