
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Adding Custom Column to Tuple list in Python
In this article, we will learn how to add a custom column in a tuple list (i.e., a list of tuples) in Python. Tuples store sequences of data enclosed in parentheses as shown below:
Tuples = (11,22,33)
And the list of tuples is represented as follows:
List of tuples = [(11, 22, 33), (44, 55, 66), (77, 88, 99)]
Suppose we have a list called "students" that stores student data (i.e., name, age) as tuples. If we represent it in the form of rows and columns, the first element of the tuples in the list represents column 1, the second element represents column 2, and so on.
Following is its visual representation -

Given task is to add a new column (i.e., grades) to the given list of tuples (i.e., students) using Python, as shown below -

Adding custom column to the list of tuple in Python
We can add a custom column to the list of tuples in the following ways -
Using zip() Function
The zip() function in Python accepts a variable number of iterable objects as parameters and rearranges the elements as per the index positions and returns them as an iterable.
Using list comprehension we can perform an operation on each element of a given iterable object and store the resultant values as a list. To add a custom column to the list of tuples -
- Combine the list of tuples and the tuple holding the values of the new column using the zip() function.
- If the given iterable contains a (nested) sequence object, the zip function will create a new tuple as (object, element), in this case ((name, age), grade).
- Create a proper list of tuples from the result of the zip() function using the list comprehension as shown below -
[(name, age, grade) for (name, age), grade in result_of_zip]
Example
The following program prints the list of tuples after adding custom columns using a list comprehension in Python.
# List of students students = [("Alice", 18), ("Bob", 17), ("Charlie", 16)] # grades of student in tuple grades = ("A", "B", "C") # Adding custom column using list comprehension new_lot = zip(students, grades) students_with_grade = [(name, age, grade) for (name, age), grade in new_lot] # Printing the result print(students_with_grade)
Output
[('Alice', 18, 'A'), ('Bob', 17, 'B'), ('Charlie', 16, 'C')]
Using the map() Function
The map() function in Python accepts a function, an iterable, and an optional variable number of iterables and returns a new iterable object by applying the specified function to each item of a given iterable.
To add a custom column to a list of tuples using the map() function, we need to pass the following as its arguments -
- A list of tuples holding the values of the original table.
- A tuple holding the elements of the new column.
- A lambda function that concatenates the given list of tuples with the tuple holding the values of the new column.
Example
The following program prints the list of tuples after adding custom columns using the map() function in Python.
# List of students students = [("Alice", 18), ("Bob", 17), ("Charlie", 16)] # grades of student in tuple grades = ("A", "B", "C") # Adding custom column using map() function students_with_grade = list(map(lambda tup, g: (tup + tuple(g)), students, grades)) # Printing the result print(students_with_grade)
Output
[('Alice', 18, 'A'), ('Bob', 17, 'B'), ('Charlie', 16, 'C')]
Conclusion
In this article, we have explored multiple methods for adding a custom column to a tuple list in Python. The list comprehension and the map() function are a concise way that allows us to achieve this task using a single line of statement, whereas the Pandas library provides a direct method.