Python | Adding N to Kth tuple element
Last Updated :
23 Feb, 2023
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 can be performed. In this, we just iterate the list to change the Kth element by predefined value N in code.
Python3
# Python3 code to demonstrate working of
# Adding N to Kth tuple element
# Using loop
# Initializing list
test_list = [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing N
N = 3
# Initializing K
K = 1
# Adding N to Kth tuple element
# Using loop
res = []
for i in range(0, len(test_list)):
res.append((test_list[i][0], test_list[i][K] + N, test_list[i][2]))
# printing result
print("The tuple after adding N to Kth element : " + str(res))
OutputThe original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after adding N to Kth element : [(4, 8, 6), (7, 7, 2), (9, 13, 11)]
Time complexity: O(n), where n is the length of the list.
Auxiliary space: O(n), where n is the length of the list
Method #2: Using list comprehension This method is having the same approach as the above method, just reduces lines of code using list comprehension functionality to make code compact by size.
Python3
# Python3 code to demonstrate working of
# Adding N to Kth tuple element
# Using list comprehension
# Initializing list
test_list = [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing N
N = 3
# Initializing K
K = 1
# Adding N to Kth tuple element
# Using list comprehension
res = [(a, b + N, c) for a, b, c in test_list]
# printing result
print("The tuple after adding N to Kth element : " + str(res))
OutputThe original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after adding N to Kth element : [(4, 8, 6), (7, 7, 2), (9, 13, 11)]
Time Complexity: O(n), where n is the number of tuples in the list.
Auxiliary Space: O(n), as we are creating a new list to store the modified tuples.
Method #3: Using map() and lambda function
This method is an alternative to using loops and list comprehension, it uses the map function to iterate over the list and a lambda function to add N to the Kth element of each tuple.
Python3
# Python3 code to demonstrate working of
# Adding N to Kth tuple element
# Using map() and lambda function
# Initializing list
test_list = [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
# printing original list
print("The original list is : " + str(test_list))
# Initializing N
N = 3
# Initializing K
K = 1
# Adding N to Kth tuple element
# Using map() and lambda function
res = list(map(lambda x: (x[0], x[K]+N, x[2]), test_list))
# printing result
print("The tuple after adding N to Kth element : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after adding N to Kth element : [(4, 8, 6), (7, 7, 2), (9, 13, 11)]
Time complexity: O(n)
Auxiliary Space: O(n)
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 | Counting Nth tuple element
Sometimes, while working with Python, we can have a problem in which we need to count the occurrence of a particular's elements. This kind of problem is quite common while working with records. Let's discuss a way in which this task can be performed. Method #1 : Using Counter() + generator expressio
5 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
Python - Assign K to Non Max-Min elements in Tuple
Sometimes, while working with Python data, we can have a problem in which we need to assign particular value as per certain condition. One of condition can be non-max, min element. This kind of task can occur in many application of day-day programming and competitive programming. Lets discuss certai
7 min read
Python | Replace tuple according to Nth tuple element
Sometimes, while working with data, we might have a problem in which we need to replace the entry in which a particular entry of data is matching. This can be a matching phone no, id etc. This has it's application in web development domain. Let's discuss certain ways in which this task can be perfor
8 min read
Python | Elementwise AND in tuples
Sometimes, while working with records, we can have a problem in which we may need to perform mathematical bitwise AND operation across tuples. This problem can occur in day-day programming. Letâs discuss certain ways in which this task can be performed. Method #1 : Using zip() + generator expression
5 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 | Remove duplicates based on Kth element tuple list
Sometimes, while working with records, we may have a problem in which we need to remove duplicates based on Kth element of a tuple in the list. This problem has application in domains that uses records as input. Let's discuss certain ways in which this problem can be solved. Method #1: Using loop Th
8 min read
Python - Count elements in record tuple
Sometimes, while working with data in form of records, we can have a problem in which we need to find the total element counts of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method
5 min read
Python - Row-wise element Addition in Tuple Matrix
Sometimes, while working with Python tuples, we can have a problem in which we need to perform Row-wise custom elements addition in Tuple matrix. This kind of problem can have application in data domains. Let's discuss certain ways in which this task can be performed.Input : test_list = [[('Gfg', 3)
4 min read