Python Language Tuple Built-in Tuple Functions


Tuples support the following build-in functions


If elements are of the same type, python performs the comparison and returns the result. If elements are different types, it checks whether they are numbers.

  • If numbers, perform comparison.
  • If either element is a number, then the other element is returned.
  • Otherwise, types are sorted alphabetically .

If we reached the end of one of the lists, the longer list is "larger." If both list are same it returns 0.

tuple1 = ('a', 'b', 'c', 'd', 'e')
tuple2 = ('1','2','3')
tuple3 = ('a', 'b', 'c', 'd', 'e')

cmp(tuple1, tuple2)
Out: 1

cmp(tuple2, tuple1)
Out: -1

cmp(tuple1, tuple3)
Out: 0

Tuple Length

The function len returns the total length of the tuple

Out: 5

Max of a tuple

The function max returns item from the tuple with the max value

Out: 'e'

Out: '3'

Min of a tuple

The function min returns the item from the tuple with the min value

Out: 'a'

Out: '1'

Convert a list into tuple

The built-in function tuple converts a list into a tuple.

list = [1,2,3,4,5]
Out: (1, 2, 3, 4, 5)

Tuple concatenation

Use + to concatenate two tuples

tuple1 + tuple2
Out: ('a', 'b', 'c', 'd', 'e', '1', '2', '3')