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
Create Dictionary Of Tuples - Python The task of creating a dictionary of tuples in Python involves mapping each key to a tuple of values, enabling structured data storage and quick lookups. For example, given a list of names like ["Bobby", "Ojaswi"] and their corresponding favorite foods as tuples [("chapathi", "roti"), ("Paraota", "I
3 min read
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
Python | Get tuple element data types Tuples can be a collection of various data types, and unlike simpler data types, conventional methods of getting the type of each element of tuple is not possible. For this we need to have different ways to achieve this task. Let's discuss certain ways in which this task can be performed. Method #1
5 min read
Python | Check order specific data type in tuple Sometimes, while working with records, we can have a problem in which we need to test for the correct ordering of data types inserted while filling forms, etc. at the backend. These tests are generally handled at frontend while web development but are recommended to be tested at backend as well. For
8 min read
Python | Convert list of tuples into list In Python we often need to convert a list of tuples into a flat list, especially when we work with datasets or nested structures. In this article, we will explore various methods to Convert a list of tuples into a list. Using itertools.chain() itertools.chain() is the most efficient way to flatten a
3 min read
Python | Dictionary to list of tuple conversion Inter conversion between the datatypes is a problem that has many use cases and is usual subproblem in the bigger problem to solve. The conversion of tuple to dictionary has been discussed before. This article discusses a converse case in which one converts the dictionary to list of tuples as the wa
5 min read