Alternative ways to create functions in Python

You can also create functions in a different way to do operations on objects (list, tuples, etc.) in a more simple way. This makes also your programming code easier to read.

Python offers also these following functions and commands that can be used with (or instead of) the "def" command:


lambda command

This command is written in this following syntax:
lambda arguments : your expression

 

You write your variable (arguments) that you will use before the double point ":". Your expression that uses your arguments is written after the double point.
The result/value of your expression is returned when you call your defined "lambda" command.

The "lambda" command can be used everywhere instead of a function defined by "def".

 

Example - My earnings calculated through my defined lambda command "e" that gets my costs "c" and my sales "s":

e = lambda k,s : s-k

Now we can call my new lambda command "e" which return the result of my expression.

print(e(1000,2000)
1000

 

map()

 
With "map()" you can apply your defined function to each item of a defined iterable object ("list", "tuple", etc.). This function "map()" returns then the results of the applied function (on the defined iterable object) as a list.

The syntax of the function:

map(func, objec)

 

Example - This application calculates the second power of given numbers and displays it as a list:

def sndpower(n): 
return n * n 

evennumbers = (0, 2, 4, 6, 8) 
results = map(sndpower, evennumbers) 
print(list(results))
{0, 4, 16, 36, 64}

You can also pass a "lambda" command instead of a function.

map(lambda n: n*n, evennumbers)


The function "map()" can also get multiple lists as arguments.

map(func, objec1, objec2, ... objecn)


 

zip()

This function "zip()" has the same functionality as "map()" except that the length of the returned list is adjusted to the length of the shortest defined iterable object.


You can also write "None" instead, if you do not want to use a function.
zip(None, myobject)

 

Example:

a = [1,2,3,4,5]
b = [10,11,12]
results = zip(a, b)

print(list(results))
[(1, 10), (2, 11), (3, 12)]

 

 

reduce()

The function "reduce()" gets the values of a sequence and returns from that a single value that was created by your defined functions. The single value could the sum, average value, etc.

You need to import the Python package "functools".

from functools import reduce


The syntax of the function:

reduce(func, seq)

 

Example - This program creates the sum of a sequence:

from functools import reduce

numbers = [4,9,17,50,130]

def createsum(x, y):
return x+y;

results = reduce(createsum, numbers)

print(results)

 

 

filter()

This function "filter()" uses your defined filter function ("func") to filter an object ("objec") which does not meet the requirement of your function "func".
The filter function "func" is a function which gets a value and returns true or false.

The syntax of the function:

filter(func, seq)


Example:

numbers = [2, 4, 6, 60, 150]

def twodigits(x):
if (x >= 10):
return True
else:
return False

results = filter(twodigits, numbers)
print(list(results))
[60, 150]

If you use "None" instead of a function, then all values are displayed that are "true".
Here is an example, where there values which are not "true":

mylist = [-10, 0, False, '', 4, 6, 60, 150]

results = filter(None, mylist)
print(list(results))
[-10, 4, 6, 60, 150]

 

Cookies make it easier for us to provide you with our services. With the usage of our services you permit us to use cookies.
Ok