Python tuple() Function – Tutorial with Examples

The tuple() function in Python is a built-in function that is used to convert a sequence or an iterable object into a tuple. It can take any object as an argument and returns a tuple object containing the elements of the original object. If the argument passed to the function is not a sequence or an iterable object, a TypeError is raised.

Syntax

tuple(iterable)

Parameters

  • iterable : This is the object that needs to be converted to a tuple. The object can be a sequence such as a list, string, range, etc. or an iterable such as a dictionary, set, etc.

Return Value

The tuple() function returns a tuple object containing the elements of the original object. If the argument passed to the function is not a sequence or an iterable object, a TypeError is raised.

Examples

Example 1: Using tuple() with a list

# using tuple() with a list
l = [1, 2, 3, 4]
t = tuple(l)
print(t)

Output.

(1, 2, 3, 4)

Example 2: Using tuple() with a string

# using tuple() with a string
s = "Hello"
t = tuple(s)
print(t)

Output.

('H', 'e', 'l', 'l', 'o')

Example 3: Using tuple() with a dictionary

# using tuple() with a dictionary
d = {'name': 'John', 'age': 30}
t = tuple(d)
print(t)

Output.

('name', 'age')

Use Cases

The tuple() function is used to convert a sequence or an iterable object into a tuple. This can be useful in cases where a sequence needs to be converted into an immutable object, such as when passing a sequence as an argument to a function. Tuples are also commonly used as keys in dictionaries because they are hashable and immutable, unlike lists which are mutable. Additionally, tuples can be used to group multiple items together as a single entity, making them useful for returning multiple values from a function or unpacking values from a tuple into separate variables.

Advantages of using tuple()

  • Immutable: Tuples are immutable, meaning the elements of a tuple cannot be changed after it has been created. This makes them useful for representing data that should not be changed, such as dates or configurations.
  • Faster than lists: Tuples are faster than lists when it comes to accessing elements and iterating over them. This is because tuples are stored in memory in a single block, making it easier for the computer to locate and access elements within the tuple.
  • Hashable: Tuples are hashable, meaning they can be used as keys in dictionaries. Lists cannot be used as keys in dictionaries because they are mutable and cannot be hashed.
  • Easy to use: Tuples are easy to create and use, making them a convenient data structure for many purposes.

Disadvantages of using tuple()

  • Immutable: The immutability of tuples can also be seen as a disadvantage, as it makes it difficult to modify the elements of a tuple after it has been created.
  • Limited functionality: Tuples have limited functionality compared to lists, as they cannot be modified in-place and do not have many built-in methods like lists do.
  • Less efficient for adding and removing elements: Tuples are less efficient than lists when it comes to adding and removing elements, as they cannot be modified in-place and new tuples must be created to make changes.

Conclusion

The tuple() function in Python is a useful built-in function for converting a sequence or an iterable object into a tuple. Tuples are immutable, fast, and hashable, making them a useful data structure for many purposes. However, their immutability and limited functionality can also be seen as disadvantages in certain cases. When deciding whether to use a tuple or a list, it is important to consider the specific requirements of the task at hand and choose the data structure that best meets those requirements.

Leave a Reply

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