Built with Tutify.io

    AIStudyLabs

    Convert List to Tuple in Python

    Convert List to Tuple in Python

    How to convert a list to a tuple in Python? You can convert a list to a tuple in python using the tuple() built-in function. The tuple() function takes the list as an argument and returns a new tuple with the same elements as the original list.

    You can convert a list to a tuple by using many ways, for example, using the tuple(), for loop, * unpacking. In this article, I will explain convert a list to a tuple by using all these methods with examples.

    <a id="h-1-quick-examples-of-convert-list-to-tuple"></a>

    1. Quick Examples of Convert List to Tuple

    If you are in a hurry, below are some quick examples of how to convert a list to a tuple.

    
    # Quick examples of converting list to tuple
    
    # Example 1: Convert list to a tuple
    mylist = [22, 42, 53, 64, 85]
    tuples = tuple(mylist)
    
    # Example 2: Convert list to tuple 
    # Using a loop inside tuple()
    my_list = [12, 34, 65, 57]
    tuples = tuple(i for i in my_list)
    
    # Example 3: Convert the list to a tuple 
    # using the * operator
    my_list = ['Spark', 'Python', 'Pandas', 'Java']
    tuples = (*my_list,)
    

    <a id="h-2-convert-the-list-to-a-tuple-in-python"></a>

    2. Convert the List to a Tuple in Python

    You can convert a list to a tuple using the built-in tuple() function. For example, you first create a list called mylist containing the integers values. Then, use the tuple() function to convert the list to a tuple and store the result in a new variable called tuples. This function returns the tuple type.

    
    # Create a list
    my_list = [22, 42, 53, 64, 85]
    
    # Convert the list to a tuple
    tuples = tuple(my_list)
    print(tuples)
    
    # Output:
    # (22, 42, 53, 64, 85)
    

    You can also create the convert function that takes the list and returns the tuple. For example, the convert() function takes a single argument my_list, which is the list that you want to convert to a tuple. Inside the function, you call the tuple() function, passing it in my_list as an argument. The tuple() function converts the list to a tuple and returns it.

    
    # Using the tuple() built-in Function
    my_list = [1, 3, 5, 7]
    
    def convert(my_list):
        return tuple(my_list)
    print(convert(my_list))
    
    # Output:
    # (1, 3, 5, 7)
    

    <a id="h-4-convert-list-to-tuple-using-for-loop"></a>

    4. Convert List to Tuple Using For Loop

    Similarly, you can convert a list to a tuple using an for loop inside function in Python. The convert() function takes a single argument my_list, which is the list that you want to convert to a tuple. Inside the function, you use a generator expression to loop over each element of the list and convert it to a tuple. The resulting tuple is returned by the function.

    
    # Convert list to tuple 
    # Using a loop inside tuple()
    my_list = [12, 34, 65, 57]
    tuples = tuple(i for i in my_list)
    print(tuples)
    
    # Output:
    # (12, 34, 65, 57)
    

    5. Using * Unpacking List Inside the Parenthesis(*list, )

    You can also use the * operator to unpack a list inside the parenthesis of a tuple. For example, you create a list of strings called my_list containing the elements 'Spark', 'Python', 'Pandas', and 'Java'. To convert this list to a tuple, use the * operator to unpack the elements of the list inside the tuple() function. you then add a comma at the end to create a tuple with a single element.

    
    # Create a list of strings
    my_list = ['Spark', 'Python', 'Pandas', 'Java']
    
    # Convert the list to a tuple 
    # using the * operator
    tuples = (*my_list,)
    print(tuples)
    
    # Output :
    # ('Spark', 'Python', 'Pandas', 'Java')
    

    Conclusion

    In this article, I have explained how to convert a list to a tuple in python by using the tuple(), for loop, * unpacking with examples.

    Happy Learning !!

    Related Articles

    References