ADVERTISEMENT
ADVERTISEMENT

Python program to update each element in the tuple list

In this program, we are given a list of tuples and an update element. We need to create a Python program to update each element of the list of tuples by the given element.
Submitted by Shivang Yadav, on December 27, 2021

With its increasing popularity Python is becoming the go-to programming language to create programs. With this small utility programs on collections are becoming more and more important. Here, is one such program where we will learn to update each element in the tuple list in Python.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Updating Each element in Tuple List

In this problem, we are given a list of tuples and an adder element to be added to each element of the list. Our program will be adding the element to each element of the tuple list.

Input:
[(12, 8), (5, 2), (1, 3, 7, 8)]
ele = 6

Output:
[(18, 14), (11, 8), (7, 9, 13, 14)]

We simply need to take each element of the list of tuples and add the given element to it. Multiple ways are used which we can perform the task, let explore them.

Method 1:

One method to solve the problem is by list comprehension to iterator the list of tuples and updating each element by adding the update element to it and then returning the updated list.

# Initializing and printing the list of tuples 

tupList = [(12, 8), (5, 2), (1, 3, 7, 8)]

print("The elements of list of tuple are " + str(tupList))
updateVal = 6

# Updating list of tuple by adding the given element
updatedList = [tuple(j + updateVal for j in tup ) for tup in tupList]

print("List after adding the update element to each element : " + str(updatedList))

Output:

The elements of list of tuple are [(12, 8), (5, 2), (1, 3, 7, 8)]
List after adding the update element to each element : [(18, 14), (11, 8), (7, 9, 13, 14)]

Method 2:

Another method to solve the problem is by using the map method to update the values using a lambda function that takes each element of tuple and added updateVal to it. All the updated map values are converted to a tuple using the tuple() method and encapsulated in a list.

# Initializing and printing the list of tuples

tupList = [(12, 8), (5, 2), (1, 3, 7, 8)]

print("The elements of list of tuple are " + str(tupList))
updateVal = 6

# Updating list of tuple by adding the given element
updatedList = [tuple(ele + updateVal for ele in tup ) for tup in tupList]

print("List after adding the update element to each element : " + str(updatedList))

Output:

The elements of list of tuple are [(12, 8), (5, 2), (1, 3, 7, 8)]
List after adding the update element to each element : [(18, 14), (11, 8), (7, 9, 13, 14)]

Python Tuple Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.