Home

Data Structures or Derived data Types in Python:

Storing multiple values of same or different types under a single name. They are collection of data.

  1. List
  2. Tuple
  3. String
  4. Dictionary
  5. Set
    In this notebook, we’ll discuss indepth on Tuples

Tuple:

Tuples are ordered collection of data. Unlike Lists they are immutable. Represented by parenthesis ‘( )’

tuple : (10, 20, 30, 'Hello', -2.0)

Each value in the tuple is called a element
order of elements: index of element
Index - may be positive or negative

Order (element) First Second Third Last
Positive Index 0 1 2 n-1
Negative Index -n -n+1 -n+2 -1

Defining a tuple:

  1. ’( )’
  2. tuple()

1. ‘( )’

l = (10, 20, 30)

add a comma ‘,’ if tuple is singleton,

t = (10,)

2. tuple()

Syntax:

tuple(iterable = None)

iterable: any data structures listed above

IN [9]

t = (10, 30)
print(t)
print(type(t))

stdout

(10, 30)
<class 'tuple'>

IN [8]

t1 = (10,)
print(t1)
print(type(t1))

stdout

(10,)
<class 'tuple'>

IN [3]

t2 = tuple()
print(t2)
print(type(t2))

stdout

()
<class 'tuple'>

Working with tuples:

  1. Concatenation
  2. Appending
  3. Multiplication

1. Concatenation:

Two tuples can be concatenated by using + operator.

IN [4]

t1 = (10, 20)
t2 = (30,)
print(t1 + t2)

stdout

(10, 20, 30)

2. Appending:

A tuples can be appended to string using += operator or by reassignment.

IN [5]

t1 = (10, 20)
t2 = (30,)
t1 += t2
print(t1)

stdout

(10, 20, 30)

3. Tuple Multiplication:

Tuple multiplication results a new tuple with repitition. It can be done using * operator.

IN [6]

t1 = (10, 20)
print(t1 * 5)

stdout

(10, 20, 10, 20, 10, 20, 10, 20, 10, 20)

Methods or Functions in tuple:

There are various methods or functions that are used to work on tuples.

  1. map
  2. filter
  3. sorted
  4. index
  5. reduce
  6. reversed
  7. len
  8. count
  9. sum
  10. max
  11. min
  12. enumerate
  13. zip

1. map:

Applies the given function to every element in a iterable.

Syntax:

map(function, iterable)

IN [10]

def sample_fn(x):
    return x + 2
t = [1, 2, 3, 4]
t = tuple(map(sample_fn, t))
print(t)

stdout

(3, 4, 5, 6)

2. filter:

Filters out the elements that match the given condition

Syntax:

filter(condition, iterable)

The condition should be given as a function definition which can be mapped to each variable.

IN [17]

t = (1, -1, 0, 3, -10, 100)
t2 = tuple(filter(lambda x: x>0, t)) # A Lambda function used for condition
print(t2)

stdout

(1, 3, 100)

3. sorted:

Sorts the given tuple and returns a copy

Syntax:

sorted(iterable, key = None, reverse = False)

IN [11]

l2 = (10, 20, 50, 0, -10, -1, 100)
l2 = tuple(sorted(l2))
print(l2)

stdout

(-10, -1, 0, 10, 20, 50, 100)

4. index:

Returns the index of the element in the tuple. If multiple elements exist, it gives the index of first occurrence. If the element is not in the tuple, it raises an error

Syntax:

tuple.index(element)

IN [12]

l = (10, 20, 30, 40, 10)
print(l.index(10))

stdout

0

5. reduce:

Sequentially applies the elements in the tuple to a function to give the final value. To use this we need to call functools module. In depth study of modules will be dealt later

Syntax:

reduce(function, tuple)

IN [13]

from functools import reduce

def add(x, y):
    return x+y

l = (10, 20, 30, 40)
c = reduce(add, l)
print(c)

stdout

100

Explanation:

Step 1: applies 10 and 20 to the function. result = 30

Step 2: applies the result and next element (30). result = 60

Step 3: applies the result and next element (40). result = 100

End is reached. so the result 100 is returned

6. reversed:

Returns iterator object of reversed tuple.

Syntax:

revrsed(sequence)

IN [22]

s = (10, 20, 30)
for i in reversed(s):
    print(i, end=' ')

stdout

30 20 10 

7. len:

Returns the length of the given iterable

Syntax:

len(iterable)

IN [14]

list2 = (10, 20, 30, -1, 123, 10.0)
print(len(list2))

stdout

6

8. count:

Returns the count of the element specified

Syntax:

tuple.count(element)

IN [15]

l = (10, 20, 30, 10, 20, 25, 20, 50)
print(l.count(20))
print(l.count(10))

stdout

3
2

9. sum:

Returns the sum elements in the tuple.

Syntax:

sum(tuple, start = 0)

IN [16]

l = (10, -10, 20, -30, 40)
print(sum(l))

stdout

30

10. max:

Returns the maximum value in the tuple.

Syntax:

max(tuple, key = None)

IN [17]

l = (10, 15, 50, 21, -7)
print(max(l))

stdout

50

IN [18]

l = (10, 15, 50, 21, -7, 7)
print(max(l, key=lambda x: x % 5)) # Maximum reminder when divided by 5

stdout

-7

11. min:

Returns minimum value in the iterable

Syntax:

min(iterable, key = None)

IN [19]

l = (10, 15, 50, 21, -7)
print(min(l))

stdout

-7

IN [20]

l = (10, 15, 50, 21, -7, 10.0)
print(min(l))

stdout

-7

12. enumerate:

Returns the enumerate object for given tuple. An enumerate object is an iterable which contains ordered pair of the form (index, value)

Syntax:

enumerate(iterable, start = 0)

IN [21]

l = (10, 20, 'Hello', 'a', -1)
print(tuple(enumerate(l)))

stdout

((0, 10), (1, 20), (2, 'Hello'), (3, 'a'), (4, -1))

13. zip:

Returns zipped object containing order pairs of elements from given iterables.

Syntax:

zip(iterables)

IN [40]

l = (10, 15, 50, 21, -7, 8)
t = ['Ten', 'Fifteen', 'Fifty', 'Twenty One', 'Negative Seven']
print(*zip(l, t))

stdout

(10, 'Ten') (15, 'Fifteen') (50, 'Fifty') (21, 'Twenty One') (-7, 'Negative Seven')

Accessing Elements in a tuple:

Elements of a tuple can be accessed using index.

Example:
Consider a tuple,

t = (10, 20, 30, 40, 50)

Length = 5

Element 10 20 30 40 50
Position 1 2 3 4 5
Positive Index 0 1 2 3 4
Negative Index -5 -4 -3 -2 -1

Calling $i^{th}$ element:
positive index: t [i-1]
negative index: t [i - 1 - length]

Slicing operator:

Used to get / set a sub-tuple of a tuple. Denoted by []

Syntax:

tuple_name[start = 0 : stop = length : step = 1]

IN [22]

t = (10, 20, 30, 40)
print(t[:3])

stdout

(10, 20, 30)

Printing the elements in a tuples:

  1. Using loops
  2. Using *

1. Looping:

Use for or while loop to access elements.

2. Using * :

Using * will convert the tuple elements into individual arguments of print method

IN [2]

# for loop to get index
l = (10, 20, 30, 40, 50)
for i in range(len(l)):
    print(l[i], end= ' ')

stdout

10 20 30 40 50 

IN [3]

# Using *
l = (10, 20, 30, 40, 50)
print(*l)

stdout

10 20 30 40 50

List Comprehension in tuples:

It follows the form of the mathematical set-builder notation unlike the use of map and filter functions. It is used to create tuples from either an existing one or a completely new tuple.

Set builder form: $\{x: x ~\rm{\epsilon~ iterable}\}$
Example:

t = tuple(expression (loops))

IN [9]

# comprehension
l = [10, 20, 30, 40, 50]
s = tuple(i**2 for i in l) # squares of elements in list l using comprehension
sq = list(map(lambda x: x**2, l)) # Using map
print(*s)
print(*sq)

stdout

100 400 900 1600 2500
100 400 900 1600 2500

Multi-dimensional tuples or matrices:

Like Lists, tuples / lists in a tuple is called a multidimensional tuple or nested tuple. They are generally used to store matrices.

Example:

t = ((10, 20, 30), (40, 50, 60))
# Two dimensional tuple

Accessing elements using index:

Syntax:

tuple_name[outermost_tuple_index][inner]...[inner_most_index]

Matrix:

A rectangular array of elements. Two dimensional lists or tuples are used to work with matrices.

Each row of matrix is stored as a list / tuple

Example:

matrix = ([10, 20, 30], (40, 50, 60))

Matrix input:

Matrix can be taken as input using loops or list comprehension.

Output:

Output is printed using for loop

Variable length argument Tuples:

The input given to variable length arguements are gathered together to form a tuple

Tuple Comparison:

Python allows usage of relational operators on Tuples.

Python compares two tuples using the elements present in them. Whenever a greater element is encounter the boolean value is returned.