Converting a list to a tuple is a common task in Python programming. In this blog post, we’ll explore different code examples that demonstrate how to convert a list to a tuple efficiently.
Example 1:
fruits = ['apple', 'banana', 'orange', 'kiwi'] fruits_tuple = tuple(fruits) print(fruits_tuple)
Output: (‘apple’, ‘banana’, ‘orange’, ‘kiwi’)
Example 2:
numbers = [1, 2, 3, 4, 5] numbers_tuple = (*numbers,) print(numbers_tuple)
Output: (1, 2, 3, 4, 5)