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 thetuple
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.
t = (1, 2, 3, 4) # tuple
1 in t
True
5 in t
False
len(t)
4
sorted(t, reverse=True)
[4, 3, 2, 1]
sum(t)
10
d = {1: 'a', 2: 'b', 3: 'c', 4: 'd'} # dict
1 in d
True
5 in d
False
'a' in d
False
len(d)
4
sorted(d) # only sorts the keys
[1, 2, 3, 4]
sum(d) # applies only on keys
10