Useful functions or Statements¶
reduce()
from functools¶
The reduce(fun,seq)
function is used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along. This function is defined in “functools” module.
Working :
- First two elements of sequence are picked and the result is obtained.
- Next step is to apply the same function to the previously attained result and the number just succeeding the second element and the result is again stored.
- This process continues till no more elements are left in the container.
- The final returned result is returned and printed on console.
in the previous code the idea to solve the problem Between Two Sets
here more information about GCD and LCM
GCD()
from math¶
The Highest Common Factor (HCF) , also called gcd, can be computed in python using a single function offered by math module and hence can make tasks easier in many situations.
Counter()
from collection¶
A Counter
is a dict
subclass for counting hashable objects. It is a collection where elements are stored as dictionary keys and their counts are stored as dictionary values. Counts are allowed to be any integer value including zero or negative counts.
for example: assume an array arr=[1,1,2,2,3]
and you are asked to find the number of occurrence of each integer, in this case you can use Counter
whihc will return a dictionary using the value as key and the count as value, like this:
the result will be:
iter()
from the build-in functions¶
Before start to talk about iter()
better start by remembering what is a Iterator and what is the difference with iterable
Python Iterators¶
An iterator is an object that contains a countable number of values, and it can be iterated upon, meaning that you can traverse through all the values.
Technically, in Python, an iterator is an object which implements the iterator protocol, which consist of the methods iter() and next().
Iterator vs Iterable¶
Lists, tuples, dictionaries, and sets are all iterable objects. They are iterable containers which you can get an iterator from.
All these objects have a iter()
method which is used to get an iterator:
setdefault()
method from Dictionaries¶
The setdefault() method returns the value of the item with the specified key.
If the key does not exist, insert the key, with the specified value, see example below
Syntax
Example
map()
Built-in function¶
Basic Syntax
map
functions expects a function object and any number of iterables like list, dictionary, etc. It executes the function_object for each element in the sequence and returns a list of the elements modified by the function object.
Example:
In the above example, map executes multiply2 function for each element in the list i.e. 1, 2, 3, 4 and returns [2, 4, 6, 8]Let’s see how we can write the above code using map and lambda.
We can pass multiple sequences to the map functions as shown below:
We can force convert the map output i.e. the map object to list as shown below:
filter()
Built-in function¶
Basic Syntax
filter function expects two arguments, function_object and an iterable. function_object returns a boolean value. function_object is called for each element of the iterable and filter returns only those element for which the function_object returns true.
Like map function, filter function also returns a list of element. Unlike map function filter function can only have one iterable as input.
Example:
Even number using filter function
Similar to map, filter function in Python3 returns a filter object or the iterator which gets lazily evaluated. Neither we can access the elements of the filter object with index nor we can use len() to find the length of the filter object.
pass
Statement¶
The pass statement does nothing. It can be used when a statement is required syntactically but the program requires no action. For example:
This is commonly used for creating minimal classes:Another place pass can be used is as a place-holder for a function or conditional body when you are working on new code, allowing you to keep thinking at a more abstract level. The pass is silently ignored:
max()
and the key
argument¶
The max()
function returns the largest of the input values.
Its syntax is as follows:
key
this is an optional argument and it receive a function, and this function is applied to every member of the iterable
from python documentation
key
(optional) It refers to the single argument function to customize the sort order. The function is applied to each item on the iterable.