Open In App

Python | Initialize tuples with parameters

Improve
Improve
Like Article
Like
Save
Share
Report

This article deals with initializing the tuples with parameters. Namely, default value, size, and specific value at a specific index. Let’s discuss certain ways in which this task can be performed.

Method #1 : Using tuple() + * operator This task can be performed using a combination of the above functionalities. In this, we extend the default values using the * operator and perform the formation of a tuple using tuple() 

Python3




# Python3 code to demonstrate working of
# Initialize tuples with parameters
# Using tuple() + * operator
 
# Initializing size
N = 6
 
# Initializing default value
def_val = 2
 
# Initializing index to add value
idx = 3
 
# Initializing value to be added
val = 9
 
# Initialize tuples with parameters
# Using tuple() + * operator
res = [def_val] * N
res[idx] = val
res = tuple(res)
 
# printing result
print("The formulated tuple is : " + str(res))


Output : 

The formulated tuple is : (2, 2, 2, 9, 2, 2)

Time Complexity: O(N), where N is the size of the tuple to be initialized.

Auxiliary Space: O(N)

Method #2: Using generator expression + tuple() This task can be performed using generator expression along with tuple() as well. The elements are created one by one on this and a specific element is initialized at a specific position using comparison. 

Python3




# Python3 code to demonstrate working of
# Initialize tuples with parameters
# Using tuple() + generator expression
 
# Initializing size
N = 6
 
# Initializing default value
def_val = 2
 
# Initializing index to add value
idx = 3
 
# Initializing value to be added
val = 9
 
# Initialize tuples with parameters
# Using tuple() + generator expression
res = tuple(val if i == idx else def_val for i in range(N))
 
# printing result
print("The formulated tuple is : " + str(res))


Output : 

The formulated tuple is : (2, 2, 2, 9, 2, 2)

Approach#2: Using the list: The approach used in the given code is to first create a list of size N with all elements initialized to the default value def_val using list comprehension. Then, the value val is inserted at index idx in the list. Finally, the list is converted to a tuple using the tuple() function.

  1. Initialize the size N, default value def_val, index idx and value to be added val.
  2. Create a list of size N with all elements initialized to def_val using list comprehension.
  3. Replace the element at index idx in the list with val.
  4. Convert the list to a tuple using the tuple() function.
  5. Print the tuple.

Python3




N = 6
def_val = 2
idx = 3
val = 9
 
tup = tuple([def_val]*N)
lst = list(tup)
lst[idx] = val
tup = tuple(lst)
 
print("The formulated tuple is:", tup)


Output

The formulated tuple is: (2, 2, 2, 9, 2, 2)

Time Complexity: O(N) because the list comprehension takes O(N) time to create a list of size N with all elements initialized to def_val.
Space Complexity: O(N) because a list of size N is created to store the elements before converting it to a tuple. The tuple itself takes O(N) space as well.



Last Updated : 03 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads