Ad

Python - Tuples

The Python tuples are sequence data type that is similar to the list. A tuple consists of a number of values separated by commas, it can store values of any type. Unlike lists, tuples are enclosed within parenthesis, tuples are immutable i.e you cannot change the elements of a tuple in place.

Example 1: use of Tuples

 t = ()
numbers = (1, 2, 3)
values = ('abcd', 786 , 2.23, 'john', 70.2  )
tinytuple = (123, 'john')

print (values)           # Prints complete tuple
print (values[0])        # Prints first element of the tuple
print (values[1:3])      # Prints elements starting from 2nd till 3rd 
print (values[2:])       # Prints elements starting from 3rd element
print (tinytuple * 2)   # Prints tuple two times
print (values  + tinytuple) # Prints concatenated tuple
('abcd', 786, 2.23, 'john', 70.200000000000003)
abcd
(786, 2.23)
(2.23, 'john', 70.200000000000003)
(123, 'john', 123, 'john')
('abcd', 786, 2.23, 'john', 70.200000000000003, 123, 'john')

Difference between List and Tuples

The main difference between lists and tuples are:

S.No List Tuple
Lists are enclosed in brackets [ ] Tuples are enclosed in parentheses ( )
List element value can be changed Tuple element value cannot be updated
List size can be grow or shrink Tuples size cannot be grow or shrink
Lists are shower than tuples. Tuples are faster than list.

Example 2: Difference between list and tuple

tuple = ( 'abcd', 786 , 2.23, 'john', 70.2  )
list = [ 'abcd', 786 , 2.23, 'john', 70.2  ]
tuple[2] = 1000    # Invalid syntax with tuple
list[2] = 1000     # Valid syntax with list

Example: Tuple

Some example regarding tuple declaration:

Example 1: Empty tuple
t = tuple()
Example 2: Single value tuple
t = (1) #treated as integer, not a tuple
print(type(t)) # integer
# to construct a tuple with one element just add a comma after the single element.
t=1, # same as t=(1,)
print(type(t)) # tuple
Example 3: Long tuples
t = (1,5,10,20,30,80.5,15.5,'user','subject') # tuple with several values
Example 4: Nested tuples
t = ('user','course', (15,16,17,18,19))
Example 5: Input tuple from user
t1=eval(input("Enter values for tuple 1")) # "abcd"
print("Tuple value = ", t1) # "abcd"
print(type(t1)) # str

t1=tuple(eval(input("Enter values for tuple 1"))) # "abcd"
print("Tuple value = ", t1) # ('a', 'b' , 'c', 'd')
print(type(t1)) # tuple

Convert Tuple to List

Python tuple is an immutable object. Hence any operation that tries to modify it (like append, remove) is not allowed.

However, you can perform modify a tuple with the help of list. First, convert tuple to list, append/remove item to list object, then convert the list back to tuple.

Example 3: Method 1 - Convert tuple into list using list constructor

t = ( 'java', 5 , 'python', 'django' )
l=list(t)
l.append('HTML')
t=tuple(l)
print(t)

Example 4: Method 2 - Convert tuple into list using unpacking

t = ( 'java', 5 , 'python', 'django' )
l=[*t]
l.append('HTML')
t=tuple(l)
print(t)

Example 5: Method 3 - Convert tuple into list using unpacking

t = ( 'java', 5 , 'python', 'django' )
l=[]
for x in t:
    l.append(x)
l.append('HTML')
t=tuple(l)
print(t)

Exercise - Python Tuple

  1. Find sum of all numbers in a tuples
  2. Find average of numbers in a tuples.
  3. Display largest number from a tuples
  4. Display smallest number from a tuples
  5. Input 10 numbers in a tuples and find sum of odd and even numbers separately.
  6. Input 10 numbers and reverse the tuples.
  7. Remove duplicate values from a tuples.
  8. Find factorial of each number stored in array.
  9. Convert tuple into list but only even numbers. Hint: for, if
  10. Remove n numbers from tuples from begining. Hint: convert into list
  11. Sort numbers in Tuples. Hint: convert into list
  12. Count total number of elements

Ad: