What is an embedded function?

Embedded functions are functions that come ready with the Python installation, unlike the functions we have created. Since these functions were previously developed by Python developers, we do not need to define these functions. E.g; These functions that we have used before, such as print(), len(), type(), input(), are embedded functions. We can directly call and use these functions without defining them. In this blog post, we will cover some important embedded functions that will be useful to us.

Some Embedded Functions

In this blog post, I will cover the most used embedded functions, if you wish, you can find other functions here.

map()

This function provides convenience in the operations you want to do with functions on variables, let me show you with an example before you get too confused, then you will understand very well. Before that, let’s see the general outline of the map() function first.

map(function,iter1,iter2)

What we call an iterator can be a list, a tuple, or any other data type.

Let’s have a list, and we square all the elements of this list.

list = [1,2,3,4,5,6,7,8,9,10]
 
def getSquare(number):
    return number**2
 
map1 = map(getSquare,list)

As you can see, we have assigned the map() function to the variable named map1 and let’s print it out immediately.

print(map1)

# <map object at 0x000002DFD0A76860>

As you can see, we got a map() object, so we can see it, so let’s convert the variable to a list type or another data type. Let’s convert it to list type.

map1 = list(map1)
print(map1)

# [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

As you can see, we have squared all the elements of the list. We said that we can apply embedded functions much more easily with the lambda() function that we have processed in our previous blog post. Let’s do the same example with the lambda function, then you will understand much better.

list = [1,2,3,4,5,6,7,8,9,10]
 
map2 = map(lambda number:number**2,list)
 
print(list(map2))

As you can see, we can do it much more easily with the lambda() function.

If we make a concise explanation, the map() function takes multiple parameters, the first being a function, and applies the function to other parameters. So it’s not limited to applying to just one data type. Let’s show it with an example.

list1 = [1,3,5,9,7,6]
list2 = [6,8,7,9,4,6]
list3 = [8,4,9,3,1,5]

map2 = map(lambda x,y,z:x*y*z,list1,list2,list3)

print(list(map2))

As you can see, we can apply the map() function on more than one data at the same time.

reduce()

The structure of this function is almost similar to the structure of the map() function. In this function, the other parameter, the list, etc., is the function that it takes as a parameter. Applies on the first two apples of data. I know you are confused, you will understand better if we show it with an example.

reduce(function,iter1,iter2)

Before explaining the example, we need to include the reduce() function in the project after the later versions, so let’s include the reduce() function in the project first.

from functools import reduce

Don’t get confused, I will cover the subject of import in the project in the next blog posts. For now, focus on the general structure of the reduce function.

list = [0,1,2,3,4,5,6]

reduce1 = reduce(lambda x,y:x+y,list)
print(reduce1)

Let me explain briefly;

Adds the first element first, then sums the result with the next element, and so on.

0+1 = 1
1+2 = 3
3+3 = 6
6+4 = 10
10+5 = 15
15+6 = 21

Let’s move on to the next function by doing another example.

from functools import  reduce
list = [1,2,3,4,5]
 
reduce1 = reduce(lambda x,y:x*y,list)
print(reduce1)
1*2 = 2
2*3 = 6
6*4 = 24
24*5 = 120

You will see that the result is the factorial of the list element.

filter()

The filter() function is almost the same as the reduce() function, but the function in the parameter given to the filter() function should only return a logical (True, False) value. As a result, the filter function returns a list of true values. In this way, it creates a filter on the list. You can think of it like query operations in SQL. You will understand very well with examples.

Let’s say we have a list of 20 numbers, let’s take the even numbers in this list. In this way, we will obtain a list with numbers that have created a filter on the list and only satisfy the condition of being an even number.

list = range(20)
 
filter1 = filter(lambda number:number%2==0,list)

print(list(filter1))

# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

You will understand much better with another example. This time, let’s have a list with numbers from 0 to 1000, let’s find the prime numbers in this list. This time, I will use a normal function, not a lambda() function, because as I mentioned before, we cannot write complex functions with the lambda() function.

list = range(100)
 
def primeNumber(number):
    i = 2
    if number ==2:
        return True
    elif number<2:
        return False
 
    else:
        while(i<number):
            if number%i==0:
                return False
            i +=1
        return True
 
filter2 = filter(primeNumber,list)
print(list(filter2))

# [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

In addition, there is another point we need to pay attention to here, the filter() function returns an object type value like the map() function, so we can convert it to a list type and access it this way.

zip()

The use of the zip() function, which is an embedded function, is slightly different from other functions. The parameters it takes in the zip() function are also not functions. In short, we can say that the zip() function does the grouping process. Let’s show it with an example.

Let’s have two different lists, the first list is the student’s school number, and the second is the student’s name, let’s group them.

numbers = [13,42,45,21,74,52,32,11]
names = ["Baransel","Batuhan","Alex","Andrew","Eva","Emma","Joe","Steve"]

zip1 =zip(numbers,names)
print(list(zip1))

# [(13, 'Baransel'), (42, 'Batuhan'), (45, 'Alex'), (21, 'Andrew'), (74, 'Eva'), (52, 'Emma'), (32, 'Joe'), (11, 'Steve')]

The zip() function returns an object value like any other function, so we typed it.

The zip() function does not only do grouping, but is also used when more than one value needs to be checked.

list1 = [1,6,8,9,25,36,14,52,69,21]
list2 = [3,5,25,14,41,36,58,60,20,30]
 
for i,j in zip(list1,list2):
    filter1 = filter(lambda i:i%3==0,list1)
    filter2 = filter(lambda j:j%5==0,list2)
 
print("multiples of 3",list(filter1))
print("multiples of 5",list(filter2))
 
# multiples of 3 [6, 9, 36, 69, 21]
# multiples of 5 [5, 25, 60, 20, 30]

In this way, we learned our zip() function. We have discussed the most used functions in the concept of embedded functions in Python.