Learning Python - 7th Day (Night - 2)

 Watched this tutorial: Click here

First of all I have learnt about filter function here.

To understand this I thought about a list. Lets say I have a list of fruits.

Banana, Apple, Black, Avocado, Blueberry

Now I want every single name of the fruit list to be run through a function.

Well I can do that with for loop, but isn't it lengthy?

Of course we have an alternative way named as filter()

For example I have this list: 

fruits = ('Banana', 'Apple', 'Black', 'Avocado', 'Blueberry')

I want every single fruit name run through this function, which will print them individually:

def myFunc(name):
print(name)

Now I want to modify all the items and run them through muFunc(name). I can firstly do it using for loop:

fruits = ('Banana', 'Apple', 'Black', 'Avocado', 'Blueberry')

def myFunc(name):
print(name)

for i in fruits:
i += '-Fresh'
myFunc(i)

Output:

Banana-Fresh

Apple-Fresh

Black-Fresh

Avocado-Fresh

Blueberry-Fresh

>> Here I added '-Fresh' after each fruit name.

But there is much efficient way to use filter for this purpose.

Which is:






Comments

Popular posts from this blog

Husband's Affair with Step Daughter Ends in Grisly Murder (True Crime Documentary)