Common Operations¶
There are some functions which can be applied on all collections. Here we will see details related to tuple
and dict
using Python as programming language.* in
– check if element exists in the tuple
in
can also be used ondict
. It checks if the key exists in thedict
.len
– to get the number of elements.sorted
– to sort the data (original collection will be untouched). Typically, we assign the result of sorting to a new collection.sum
,min
,max
, etc – arithmetic operations. In case ofdict
, the operations will be performed on key.- There can be more such functions.
In [1]:
t = (1, 2, 3, 4) # tuple
In [2]:
1 in t
Out[2]:
True
In [3]:
5 in t
Out[3]:
False
In [4]:
len(t)
Out[4]:
4
In [5]:
sorted(t, reverse=True)
Out[5]:
[4, 3, 2, 1]
In [6]:
sum(t)
Out[6]:
10
In [7]:
d = {1: 'a', 2: 'b', 3: 'c', 4: 'd'} # dict
In [8]:
1 in d
Out[8]:
True
In [9]:
5 in d
Out[9]:
False
In [10]:
'a' in d
Out[10]:
False
In [11]:
len(d)
Out[11]:
4
In [12]:
sorted(d) # only sorts the keys
Out[12]:
[1, 2, 3, 4]
In [13]:
sum(d) # applies only on keys
Out[13]:
10