Python - Change Datatype of Tuple Values
Last Updated :
13 Mar, 2023
Sometimes, while working with set of records, we can have a problem in which we need to perform a data type change of values of tuples, which are in its 2nd position, i.e value position. This kind of problem can occur in all domains that include data manipulations. Let's discuss certain ways in which this task can be performed.
Input : test_list = [(44, 5.6), (16, 10)]
Output : [(44, '5.6'), (16, '10')]
Input : test_list = [(44, 5.8)]
Output : [(44, '5.8')]
Method #1 : Using enumerate() + loop This is brute force way in which this problem can be solved. In this, we reassign the tuple values, by changing required index of tuple to type cast using appropriate datatype conversion functions.
Python3
# Python3 code to demonstrate working of
# Change Datatype of Tuple Values
# Using enumerate() + loop
# initializing list
test_list = [(4, 5), (6, 7), (1, 4), (8, 10)]
# printing original list
print("The original list is :"
+ str(test_list))
# Change Datatype of Tuple Values
# Using enumerate() + loop
# converting to string using str()
for idx, (x, y) in enumerate(test_list):
test_list[idx] = (x, str(y))
# printing result
print("The converted records :"
+ str(test_list))
OutputThe original list is :[(4, 5), (6, 7), (1, 4), (8, 10)]
The converted records :[(4, '5'), (6, '7'), (1, '4'), (8, '10')]
Time Complexity: O(n2) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #2 : Using list comprehension The above functionality can also be used to solve this problem. In this, we perform similar task as above method, just in one liner way using list comprehension.
Python3
# Python3 code to demonstrate working of
# Change Datatype of Tuple Values
# Using list comprehension
# initializing list
test_list = [(4, 5), (6, 7), (1, 4), (8, 10)]
# printing original list
print("The original list is :"
+ str(test_list))
# Change Datatype of Tuple Values
# Using list comprehension
# converting to string using str()
res = [(x, str(y)) for x, y in test_list]
# printing result
print("The converted records :"
+ str(res))
OutputThe original list is :[(4, 5), (6, 7), (1, 4), (8, 10)]
The converted records :[(4, '5'), (6, '7'), (1, '4'), (8, '10')]
Time Complexity: O(n*n) where n is the number of elements in the in the list “test_list”. The list comprehension is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the in the list “test_list”.
Similar Reads
Python | Convert tuple to float value Sometimes, while working with tuple, we can have a problem in which, we need to convert a tuple to floating-point number in which first element represents integer part and next element represents a decimal part. Let's discuss certain way in which this can be achieved. Method : Using join() + float()
3 min read
Python | Type conversion in dictionary values The problem of conventional type conversion is quite common and can be easily done using the built-in converters of python libraries. But sometimes, we may require the same functionality in a more complex scenario vis. for keys of list of dictionaries. Let's discuss certain ways in which this can be
4 min read
Python - Check if variable is tuple We are given a variable, and our task is to check whether it is a tuple. For example, if the variable is (1, 2, 3), it is a tuple, so the output should be True. If the variable is not a tuple, the output should be False.Using isinstance()isinstance() function is the most common and Pythonic way to c
2 min read
Python - Change type of key in Dictionary list Sometimes, while working with data, we can have a problem in which we require to change the type of particular keys' value in list of dictionary. This kind of problem can have application in data domains. Lets discuss certain ways in which this task can be performed. Input : test_list = [{'gfg': 9,
6 min read
Convert Dictionary to List of Tuples - Python Converting a dictionary into a list of tuples involves transforming each key-value pair into a tuple, where the key is the first element and the corresponding value is the second. For example, given a dictionary d = {'a': 1, 'b': 2, 'c': 3}, the expected output after conversion is [('a', 1), ('b', 2
3 min read