Python | Set 4 (Dictionary, Keywords in Python)
In the previous two articles (Set 2 and Set 3), we discussed the basics of python. In this article, we will learn more about python and feel the power of python.
Dictionary in Python
In python, the dictionary is similar to hash or maps in other languages. It consists of key-value pairs. The value can be accessed by a unique key in the dictionary.
Python3
# Create a new dictionaryd = dict() # or d = {}# Add a key - value pairs to dictionaryd['xyz'] = 123d['abc'] = 345# print the whole dictionaryprint (d)# print only the keysprint (d.keys())# print only valuesprint (d.values())# iterate over dictionaryfor i in d : print ("%s %d" %(i, d[i]))# another method of iterationfor index, key in enumerate(d): print (index, key, d[key])# check if key existprint ('xyz' in d)# delete the key-value pairdel d['xyz']# check againprint ("xyz" in d) |
Output:
{'xyz': 123, 'abc': 345}
['xyz', 'abc']
[123, 345]
xyz 123
abc 345
0 xyz 123
1 abc 345
True
Falsebreak, continue, pass in Python
- break: takes you out of the current loop.
- continue: ends the current iteration in the loop and moves to the next iteration.
- pass: The pass statement does nothing. It can be used when a statement is required. syntactically but the program requires no action.
It is commonly used for creating minimal classes.
Python3
# Function to illustrate break in loopdef breakTest(arr): for i in arr: if i == 5: break print (i) # For new line print("")# Function to illustrate continue in loopdef continueTest(arr): for i in arr: if i == 5: continue print (i) # For new line print("")# Function to illustrate passdef passTest(arr): pass # Driver program to test above functions# Array to be used for above functions:arr = [1, 3 , 4, 5, 6 , 7]# Illustrate breakprint ("Break method output")breakTest(arr)# Illustrate continueprint ("Continue method output")continueTest(arr)# Illustrate pass- Does nothingpassTest(arr) |
Output:
Break method output 1 3 4 Continue method output 1 3 4 6 7
map, filter, lambda
- map: The map() function applies a function to every member of iterable and returns the result. If there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables.
- filter: It takes a function returning True or False and applies it to a sequence, returning a list of only those members of the sequence for which the function returned True.
- lambda: Python provides the ability to create a simple (no statements allowed internally) anonymous inline function called lambda function. Using lambda and map you can have two for loops in one line.
Python3
# python program to test map, filter and lambdaitems = [1, 2, 3, 4, 5]#Using map function to map the lambda operation on itemscubes = list(map(lambda x: x**3, items))print(cubes)# first parentheses contains a lambda form, that is # a squaring function and second parentheses represents# calling lambdaprint( (lambda x: x**2)(5))# Make function of two arguments that return their productprint ((lambda x, y: x*y)(3, 4))#Using filter function to filter all# numbers less than 5 from a listnumber_list = range(-10, 10)less_than_five = list(filter(lambda x: x < 5, number_list))print(less_than_five) |
Output:
[1, 8, 27, 64, 125] 25 12 [-10, -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4]
For more clarity about map, filter, and lambda, you can have a look at the below example:
Python3
# code without using map, filter and lambda# Find the number which are odd in the list# and multiply them by 5 and create a new list# Declare a new listx = [2, 3, 4, 5, 6]# Empty list for answery = []# Perform the operations and print the answerfor v in x: if v % 2: y += [v*5]print(y) |
Output:
[15, 25]
The same operation can be performed in two lines using map, filter, and lambda as :
Python3
# above code with map, filter and lambda# Declare a listx = [2, 3, 4, 5, 6]# Perform the same operation as in above posty = list(map(lambda v: v * 5, filter(lambda u: u % 2, x)))print(y) |
Output:
[15, 25]
References :
- https://docs.python.org/2/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops
- http://www.u.arizona.edu/~erdmann/mse350/topics/list_comprehensions.html
This article is contributed by Nikhil Kumar Singh
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above



