Course Content

Python Comprehensions | Python Tutorials For Absolute Beginners In Hindi #73

Comprehensions in Python can be defined as the Pythonic way of writing code. Using comprehension, we compress the code so it takes less space. Comprehension in Python converts the four to five lines of code into a one-liner. In this tutorial, we will see the ways to write the code we used to write earlier in four to five lines, in just one line.

Comprehension’s importance comes in the scenarios when the project is too big, for example, Google is made up of 2 billion lines of code, Facebook is made from 62 million lines of code, Windows 10 has roughly 50 million lines of code. So in such scenarios, comprehension has to be implemented as much as we can so that the lines of code decreases and the efficiency increases. 

In Python 2.0, we only had the option of list comprehensions, but now after 3.0 version, we can also apply comprehension on dictionary and sets. We will discuss all three of them in this tutorial along with its implementation on generators. We have already discussed all of these topics before in our previous tutorials. I will provide you with the links of them so that  if you have somehow missed any of those, you may watch them or give the description a read. 

We will now learn ways to create a comprehensive way of implementing the functions. The concept is the same; the only difference comes in writing them i.e. in a more compact form. To understand the working, you can refer to the tutorial links given above as our focus will be more towards syntax in this part.

List as ordinarily are written as such:

 listA = []
 for a in range(50):
     if a%5==0:

While it can be written in a one liner format using comprehension as such:

listA = [a for a in range(50) if i%5==0]

The compressed code works exactly like the one above but with more precision.

Set comprehension works exactly the same way as List comprehension. The syntax is almost the same two, except for the brackets i.e. set uses curly brackets. The main difference arrives while printing the items as a set will only print the same items once. 

 alpha = {alpha for alpha in ["a", "a", "b", "c", "d", "d"]}

The output will be: {'a', 'b', 'c', 'd'}

In the case of the dictionary, it has more benefits as we can alter the sequence of the dictionary by printing the values before the keys. We can also write conditional statements, that in the case of dictionary consumes 8-9 lines in just a single one. The syntax for a dictionary using ordinary syntax is:

Normaldict = {
  0 : "item0",
  1 : "item1",
  2 : "item2",
  3 : "item3",
  4 : "item4",

And the more compact one is:

Compdict = {i:f"Item {i}" for i in range(5)}

We can implement comprehension on generators too. We discussed Generators in the previous tutorial, i.e. Tutorial #72. We will again see its syntax here:

def gener(n):
    for i in range(n):
        yield i

a = gener(5)

We can also create a one liner for generators too by following the syntax below.

gener = (i for i in range(n))
a = gener(5)


Code as described/written in the video

# ls = []
# for i in range(100):
#     if i%3==0:
#         ls.append(i)

# ls = [i for i in range(100) if i%3==0]
# print(ls)

# dict1 = {i:f"item {i}" for i in range(1, 10001) if i%100==0}
# dict1 = {i:f"Item {i}" for i in range(5)}
# dict2 = {value:key for key,value in dict1.items()}
# print(dict1,"\n", dict2)

# dresses = [dress for dress in ["dress1", "dress2","dress1",
#                                "dress2","dress1", "dress2"]]
# print(type(dresses))

evens = (i for i in range(100) if i%2==0)
# print(evens.__next__())

# for item in evens:
#     print(item)


sanchita 10 months, 3 weeks ago
n = int(input("enter the total number in a list:"))
for i in range(0,n):
print("in which form do you want to convert")
a = str(input("list,set,dict"))

if a=="dict":
        dict1 = {lst: f"element{lst}" for i in range(0,n + 1)}

sir i m having confusion with the set

ompatel303 10 months ago
Can you review my code and i will review yours? 
here is my code
ompatel303 10 months ago
while True:
    l1 = []
    n = int(input("How many items do you want to add?"))
    for i in range(n):
        zebra = input("Enter the things you want to add : ")

    print("1 : List Comprehension , 2 : Set Comprehension, 3: Dictionary comprehension ")
    choice = int(input("Which type of comprehension you want to use?"))

    if choice == 1:
        user_list1 = [item for item in l1]
    elif choice == 2:
        user_list2 = {item for item in l1}
        user_list3 = {i:str(l1[i]) for i in range(len(l1)) }


No downloadable resources for this video. If you think you need anything, please post it in the QnA!

Course Announcements

Any Course related announcements will be posted here