Open In App

Python – Constant Multiplication to Nth Column

Last Updated : 09 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 K can be multiplied to Nth 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 Nth element by predefined value K in code. 

Python3




# Python3 code to demonstrate working of
# Constant Multiplication to Nth Column
# 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 = 1
 
# Initializing K
K = 3
 
# Constant Multiplication to Nth Column
# Using loop
res = []
for i in range(0, len(test_list)):
    res.append((test_list[i][0], test_list[i][N] * K, test_list[i][2]))
 
# printing result
print("The tuple after multiplying K to Nth element : " + str(res))


Output : 

The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after multiplying K to Nth element : [(4, 15, 6), (7, 12, 2), (9, 30, 11)]

Time Complexity: O(n) where n is the number of elements in the list “test_list”. loop performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in 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
# Constant Multiplication to Nth Column
# 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 = 1
 
# Initializing K
K = 3
 
# Constant Multiplication to Nth Column
# Using list comprehension
res = [(a, b * K, c) for a, b, c in test_list]
 
# printing result
print("The tuple after multiplying K to Nth element : " + str(res))


Output : 

The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after multiplying K to Nth element : [(4, 15, 6), (7, 12, 2), (9, 30, 11)]

The time complexity of this code is O(n), where n is the length of the input list test_list, as the code iterates through the list once to create the new list res.

The space complexity is also O(n), as a new list res is created that has the same length as the input list test_list. 

Method #3: Using map()

This method uses the built-in map() function to apply a lambda function to each tuple in the list, which multiplies the Nth element by the constant value K.

Python3




# Python3 code to demonstrate working of
# Constant Multiplication to Nth Column
# Using map()
 
# 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 = 1
 
# Initializing K
K = 3
 
# Constant Multiplication to Nth Column
# Using map()
res = list(map(lambda x: (x[0], x[N] * K, x[2]), test_list))
 
# printing result
print("The tuple after multiplying K to Nth element : " + str(res))
 
#This code is contributed by Edula Vinay Kumar Reddy


Output

The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after multiplying K to Nth element : [(4, 15, 6), (7, 12, 2), (9, 30, 11)]

Time complexity: O(n)
Auxiliary Space: O(n)

Method 4: Using list slicing and unpacking. 

Approach:

  • Initialize a new empty list to store the modified tuples.
  • Loop through each tuple in the original list.
  • Slice the tuple to extract its first, second, and third elements.
  • Multiply the second element with K and replace it in the sliced tuple.
  • Pack the modified tuple back and append it to the new list.
  • Return the new list.

Below is the implementation of the above approach:

Python3




# Python3 code to demonstrate working of
# Constant Multiplication to Nth Column
# Using list slicing and unpacking
 
# 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 = 1
 
# Initializing K
K = 3
 
# Constant Multiplication to Nth Column
# Using list slicing and unpacking
res = []
for tup in test_list:
    first, second, third = tup[:N], tup[N]*K, tup[N+1:]
    modified_tup = (*first, second, *third)
    res.append(modified_tup)
 
# printing result
print("The tuple after multiplying K to Nth element : " + str(res))


Output

The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after multiplying K to Nth element : [(4, 15, 6), (7, 12, 2), (9, 30, 11)]

The time complexity of this approach is O(n), where n is the number of tuples in the input list.
The auxiliary space complexity is also O(n), since we’re creating a new list to store the modified tuples.

Method 5: Using the numpy library. 

Here are the steps to implement this method:

  1. Import the numpy library.
  2. Convert the list of tuples test_list to a numpy array.
  3. Multiply the Nth column of the array by K using the numpy.multiply() function.
  4. Replace the Nth column of the array with the modified column.
  5. Convert the numpy array back to a list of tuples using the tolist() method.

Python3




import numpy as np
 
# 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 = 1
 
# Initializing K
K = 3
 
# Constant Multiplication to Nth Column
# Using numpy
arr = np.array(test_list)
arr[:,N] = np.multiply(arr[:,N], K)
res = arr.tolist()
 
# printing result
print("The tuple after multiplying K to Nth element : " + str(res))


Output:

The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after multiplying K to Nth element : [[4, 15, 6], [7, 12, 2], [9, 30, 11]]

Time complexity: O(NM), where N is the number of tuples and M is the number of elements in each tuple. This is because we need to iterate over all the elements in each tuple and multiply the Nth element by K.
Auxiliary space: O(NM), where N is the number of tuples and M is the number of elements in each tuple. This is because we are creating a numpy array with the same shape as the original list of tuples.

Method 6: Using pandas library

  1. Import the pandas library.
  2. Convert the original list to a pandas DataFrame using the pd.DataFrame() function.
  3. Multiply the Nth column by K using DataFrame multiplication.
  4. Convert the result back to a list of tuples using the DataFrame.values.tolist() function.

Python3




# Python3 code to demonstrate working of
# Constant Multiplication to Nth Column
# Using pandas library
 
# import pandas library
import pandas as pd
 
# 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 = 1
 
# Initializing K
K = 3
 
# convert list to pandas DataFrame
df = pd.DataFrame(test_list)
 
# multiply Nth column by K
df[N] = df[N] * K
 
# convert DataFrame back to list of tuples
res = df.values.tolist()
 
# printing result
print("The tuple after multiplying K to Nth element : " + str(res))


OUTPUT:
The original list is : [(4, 5, 6), (7, 4, 2), (9, 10, 11)]
The tuple after multiplying K to Nth element : [[4, 15, 6], [7, 12, 2], [9, 30, 11]]

Time complexity: O(n), where n is the number of elements in the list.
Auxiliary space: O(n), where n is the number of elements in the list (due to the conversion to a DataFrame and back).



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads