Lesson 12: zip()

zip() takes two or more lists and combines them for you.

Similar to map and filter, you need to use it inside list function if you want to display meaningful values. Otherwise it will just print the memory address of the function object.

zip() will return tuples in a list, something that looks like:
[(1,2),(3,4),(4,5)]
After that it’s very east to address the items. First item comes from the first list that’s being merged and second item in the tuple comes from the second list.

Let’s see some examples:

Function 1: zip()

zip() function in Python will take two or more lists as argument and it will return tuples consisted of pairs or group of threes, fours… depending how many lists were passed as argument.

Used Where?

When processing lists, if you need to merge them quickly zip() offers a great solution for that.

Syntax do(s)

1) just use zip() function with multiple lists inside parenthesis separated with comma.

Syntax don't(s)

1) 

Example 1: zip()

Let’s say we got 2 lists with related values:
First city names, Second their PM2.5 air pollution values of 5 most populated cities in the world.

>>> lst1 = [“Kanpur”, “Faridabad”, “Bhutan”, “Gaya”, “Varanasi”]
>>> lst2 = [173, 171, 150, 149, 146]
>>> lst_merged = zip(lst1, lst2)
>>> print(list(lst_merged))

[(‘Kanpur’, 173), (‘Faridabad’, 171), (‘Bhutan’, 150), (‘Gaya’, 149), (‘Varanasi’, 146)]

Sadly, the list demonstrates South Asia’s ongoing struggle with pollution. PM2.5 is short for Particulate Matter that’s 2.5 micrometers or smaller in diameter and shows air pollution. 2.5 micrometers or very small particles are very dangerous and can be inhaled by humans or animals.

Example 3: zip() with 3 lists

Adding the country data as a 3 list might make sense.

>>> lst1 = [“Kanpur”, “Faridabad”, “Bhutan”, “Gaya”, “Varanasi”]
>>> lst2 = [173, 171, 150, 149, 146]
>>> lst3 = [“India”, “India”, “Bhutan”, “India”, “India”]
>>> lst_merged = zip(lst1, lst2, lst3)

>>> print(list(lst_merged))

[(‘Kanpur’, 173, ‘India’), (‘Faridabad’, 171, ‘India’), (‘Bhutan’, 150, ‘Bhutan’), (‘Gaya’, 149, ‘India’), (‘Varanasi’, 146, ‘India’)]

Now each tuple is consisted of 3 related elements coming from each list.

Printing the function object

You might at some point encounter a strange output when you’re learning Python. This happens when you try to print function objects directly without without calling it with any value.

Example 4: printing the function

>>> a = lambda x: print(“Hi”)
>>> print(a)

at 0x00000163154BB1E0>

But what is the last printed line that looks weird?

This is because we are trying to print the function itself directly without calling it for any values. What happens is, Python prints the unique memory address of our function. That’s why it looks slightly weird. But later it grows on you 🙂

Example 5: To print or not to print

>>> a = lambda x: print(“Hi”)
>>> a()

at 0x00000163154BB1E0>

Another point is that you actually don’t have to call your function inside a print() function when your function already has print in it.

Just call your function and it will do the printing for you.

Example 6: lambda with two arguments

You can also pass two arguments to your lambda:

>>> a = lambda x, y: x*y
>>> a(11, 6)

66

Tips (optional)

1- One of the most useful aspect of lambda is the ability to pass it as an argument inside some other functions.

We will look at each of these functions in detail in next lessons individually so you don’t have to ponder too much.

But it might be beneficial to take a look at them here to get familiar in advance.

 

Example 7: .sort() with lambda

.sort() is a list method that sorts the list’s elements directly.

Also it can take a function as an argument and base its sorting algorithm on this.

Simply pass your lambda function as an argument inside .sort()’s parenthesis.

>>> lst = [1, 5, 66, 7]
>>> lst.sort(key=lambda x: x)
print(lst)

[1, 5, 7, 66]

if you’d like to use a function as an argument in .sort() you need to use it with “key=” keyword.

In this example lambda function is more like a place holder as its statement is the variable itself unchanged. (lambda x: x)

It basically says, sort based on each element’s value, which is the default tendency of .sort() function anyway.

But the syntax is nicely demonstrated this way.

Example 8: sorted() with lambda

sorted() is not a method but a builtin function. It’s main difference from .sort() method is that it won’t change the original list it will simply output a new list. You have to assign this new list to a variable if you’d like to save it.

>>> lst = [1, 5, 66, 7]
>>> lst_sorted = sorted(lst, key=lambda x: x)

[1, 5, 7, 66]

Again you can see lambda is passed with “key=”.

Another syntactical difference from .sort() is that, since you’re not calling sorted() on a list directly you have to pass the list’s name inside its parenthesis before the “key=” parameter.

Advanced Concepts (Optional)

1- You can even include an if statement in your lambda although its stretching the practical mini use intentions of lambda, syntactically it’s correct and you may find useful applications for it.

Let’s see an example.

Example 11: Lambda with if

if statement in lambda

>>> a = lambda x: print(“Aloha!”) if x==”Hawaii” else print(“Ciao!”)
>>> print(“Hawaii”)
>>> print(“London”)

Aloha!
Ciao!

Exercise 12

Lesson 13

map()

Autumnal Forest Renewal - nicholas bott


Nicholas Bott

Autumnal Forest Renewal

Autumnal Forest Renewal by Nicholas Bott