Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Initialize tuples with parameters

Python | Initialize tuples with parameters

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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments