Python
Python
Just like how str(42) will return '42', the string representation of the integer 42, the functions list()
and tuple() will return list and tuple versions of the values passed to them. Enter the following
into the interactive shell, and notice that the return value is of a different data type than the
value passed:
>>> type(('hello',))
<class'tuple'>
>>> type(('hello'))
<class 'str'>
Using a loop
This method is similar to the above one, but in this case, we do not send the
complete list as an argument whereas we retrieve each element from the list
using a for loop to send it as an argument for the tuple() built-in function.
Example
The following is an example code to convert a list to a tuple using a loop.
list_names=['Meredith', 'Kristen', 'Wright', 'Franklin']
tuple_names= tuple(i for i in list_names)
print(tuple_names)
print(type(tuple_names))
Output
('Meredith', 'Kristen', 'Wright', 'Franklin')
<class 'tuple'>