KnowledgeBoat Logo

Computer Science

How is a tuple containing just one element created ?

Python Tuples

8 Likes

Answer

There are two ways of creating single element tuple:

  1. By enclosing the element in parentheses and adding a comma after it.
    Example: t = (a,)
  2. By using the built-in tuple type object (tuple( )) to create tuples from sequences:
    Example:
    t = tuple([1])
    Here, we pass a single element list to the tuple function and get back a single element tuple.

Answered By

6 Likes


Related Questions