The tuple function

The tuple function creates a new tuple from the object specified in the parameter.

Syntax

pythontuple(the object from which we create a tuple)

Example

Let's make a tuple using the tuple function:

pythontxt = 'abcde'
tlp = tuple(txt)
print(tlp)

Result of code execution:

python('a', 'b', 'c', 'd', 'e')

Example

Now let's make a dictionary from the number:

pythonnum = 1234
tlp = tuple(num)
print(tlp)

After executing the code, the function will return us an error, since the number is not an iterable object:

errorTypeError: 'int' object is not iterable

Example

Let's use the tuple function to make a tuple from a list:

pythonlst = ['a', 'b', 'c', 'd']
tlp = tuple(lst)
print(tlp)

Result of code execution:

python('a', 'b', 'c', 'd')

See also

  • function list,
    which creates a list
  • function dict,
    which creates a dictionary