Python | Convert a list into a tuple

Given a list, write a Python program to convert the given list into a tuple.

Examples:

Input : [1, 2, 3, 4]
Output : (1, 2, 3, 4)

Input : ['a', 'b', 'c']
Output : ('a', 'b', 'c')

 
Appraoch #1 : Using tuple(list_name).

Typecasting to tuple can be done by simply using tuple(list_name).

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to convert a 
# list into a tuple
def convert(list):
    return tuple(list)
  
# Driver function
list = [1, 2, 3, 4]
print(convert(list))

chevron_right


Output:

(1, 2, 3, 4)

 
Appraoch #2 :
A small variation to the above appraoch is to use a loop inside tuple() .

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to convert a 
# list into a tuple
def convert(list):
    return tuple(i for i in list)
  
# Driver function
list = [1, 2, 3, 4]
print(convert(list))

chevron_right


Output:

(1, 2, 3, 4)

 
Appraoch #3 : Using (*list, )
This essentially unpacks the list l inside a tuple literal which is created due to the presence of the single comma (, ). This approach is a bit faster but suffers from readability.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to convert a 
# list into a tuple
def convert(list):
    return (*list, )
  
# Driver function
list = [1, 2, 3, 4]
print(convert(list))

chevron_right


Output:

(1, 2, 3, 4)


My Personal Notes arrow_drop_up