Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Convert List to Tuple in Python

  • 27 Feb, 2025
  • Com 0
Python List to Tuple Conversion

The easiest and most efficient way to convert a list into a tuple is to use the built-in tuple() Constructor, which accepts an iterable(list) as its argument and returns a new tuple containing the elements of that iterable(list).

Using tuple() function to convert list to tuple

main_list = [11, 18, 19, 21, 46]

print(main_list)
# Output: [11, 18, 19, 21, 46]

print(type(main_list))
# Output: <class 'list'>

main_tuple = tuple(main_list)

print(main_tuple)
# Output: (11, 18, 19, 21, 46)

print(type(main_tuple))
# Output: <class 'tuple'>

Once the tuple is created, you cannot modify it like in the list.

Empty list to tuple

If you try to convert an empty list to a tuple, it will return an empty tuple.

main_list = []

print(main_list)
# Output: []

print(type(main_list))
# Output: <class 'list'>

main_tuple = tuple(main_list)

print(main_tuple)
# Output: ()

print(type(main_tuple))
# Output: <class 'tuple'>

Nested list

If you apply the basic tuple() conversion to a nested list, the inner elements will remain lists, while the outer list will become a tuple.

nested_list = [[1, 2], [3, 4]]

print(nested_list)
# Output: [[1, 2], [3, 4]]

print(type(nested_list))
# Output: <class 'list'>

main_tuple = tuple(nested_list)

print(main_tuple)
# Output: ([1, 2], [3, 4])

print(type(main_tuple))
# Output: <class 'tuple'>

The above output shows that we did not get a tuple of tuples but a tuple of lists. 

If we want to make the inner lists tuples, too, we need to use processed approach.

nested_list = [[1, 2], [3, 4]]

print(nested_list)
# Output: [[1, 2], [3, 4]]

print(type(nested_list))
# Output: <class 'list'>


def deep_list_to_tuple(lst):
    return tuple(deep_list_to_tuple(x) if isinstance(x, list) else x for x in lst)


deep_tuple = deep_list_to_tuple(nested_list)

print(deep_tuple)
# Output: ((1, 2), (3, 4))

print(type(deep_tuple))
# Output: <class 'tuple'>

Here are three alternate ways to convert a list to a tuple:

  1. Using the unpacking operator(*)
  2. Using map() and lambda
  3. Using the processed tuple approach

Alternate way 1: Using the Unpacking Operator(*)

You can use the unpacking operator (*) to unpack a list and then repack it into a tuple.

This method is short and works well for small lists.

Using the Unpacking Operator(*)

main_list = [11, 18, 19, 21, 46]

print(main_list)
# Output: [11, 18, 19, 21, 46]

print(type(main_list))
# Output: <class 'list'>

main_tuple = (*main_list, )

print(main_tuple)
# Output: (11, 18, 19, 21, 46)

print(type(main_tuple))
# Output: <class 'tuple'>

Alternate way 2: Using map() and lambda

The map() function applies the lambda function to each list element, returning each element as it is.

Then, tuple() converts the iterable returned by map() into a tuple.

Using map() and lambda

main_list = [11, 18, 19, 21, 46]

print(main_list)
# Output: [11, 18, 19, 21, 46]

print(type(main_list))
# Output: <class 'list'>

main_tuple = tuple(map(lambda x: x, main_list))

print(main_tuple)
# Output: <class 'list'>

print(type(main_tuple))
# Output: <class 'tuple'>

Alternate way 3: Processed tuple approach

The processed approach mainly modifies elements while converting a list to a tuple.

main_list = [1, 2, 3, 4]

print(main_list)
# Output: [1, 2, 3, 4]

print(type(main_list))
# Output: <class 'list'>

processed_tuple = tuple(x * 2 for x in main_list)

print(processed_tuple)
# Output: (2, 4, 6, 8)

print(type(processed_tuple))
# Output: <class 'tuple'>

The above output is a converted tuple and doubles each element.

That’s it!

Post Views: 34
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Convert a String to Numpy Array in Python
How to Convert Tuple to List in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend