DEV Community

loading...
Cover image for โšก Superb Comprehensions โšกIn Python Using List and Sets

โšก Superb Comprehensions โšกIn Python Using List and Sets

sudarshansb143 profile image Sudarshan Sawandkar ใƒป3 min read

๐Ÿ“ข TL:DR :

Dealing with data structures is not a big deal if you are using python, isn't it ?

If it is a big deal, then take look at conversation.
Here, we have 2 noob coders .
๐Ÿ˜Ž Chintu and ๐Ÿ˜ทMintu. ๐Ÿ˜ท Mintu is technical client in the room and Chintu is A-K-A a developer. Hence, ๐Ÿ˜ŽChintu is implementing the requirements of his client, which are changing very fast (as we all know).


[ M : Mintu, C: Chintu]

๐Ÿ˜ท M : I want to generate a list of even numbers from 1 to N, but not with classical for.... in loop with range() and all that. Instead with something different

๐Ÿ˜Ž C : Ok ! this is my try

n = 100
allSum = [num  for num in range(1, n+ 1) if num % 2 == 0 ]
print(allSum)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ˜ท M : Ooops ! I want square of each number, once they are in a list....

๐Ÿ˜Ž C : No worries

n = 100
allSum = [num**2  for num in range(1, n+ 1) if num % 2 == 0 ]
print(allSum)

Enter fullscreen mode Exit fullscreen mode

๐Ÿ˜ท M : Yahh... It will be pretty nice if it contains original numbers also

๐Ÿ˜Ž C : Ok ! Here we go

n = 100
allSum = [[num, num**2]  for num in range(1, n+ 1) if num % 2 == 0 ]
print(allSum)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ˜ท M : Awesome !!! but, now I want dictionary of original and squared number instead of nested lists

๐Ÿ˜Ž C : Fine ! I have this

n = 100
allSum = [{num: num**2}  for num in range(1, n+ 1) if num % 2 == 0 ]
print(allSum)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ˜ท M : Great ! but, now I think all should be in a SET, but by using your previous code

๐Ÿ˜Ž C : Ok Fine ! see this

n = 100
allSum = {num**2 for num in range(1, n+ 1) if num % 2 == 0 }
print(allSum)
Enter fullscreen mode Exit fullscreen mode

๐Ÿ˜ท M : Cool ! But, now I want iterator instead of the whole list at once

๐Ÿ˜Ž C : OK Sir !

n = 100
allSum = iter([num**2 for num in range(1, n+ 1) if num % 2 == 0])
print(next(allSum ))
Enter fullscreen mode Exit fullscreen mode

๐Ÿ˜ท M : can you explain why we should prefer ** list iterator ** instead of the normal list ?

๐Ÿ˜Ž C : Yup ! List iterator are kind of generator object in nature, which are used to comprehensively yield the values when we call the next() on the object of iterator. The real benefit of creating generator is, that they don't consume memory before actual yielding / producing the value. This saves a lot of runtime memory and gives out programme a free space to use.

Application of this would be like, if we have a database transaction which requires millions of rows to complete the process. In this scenario, if we loaded the data using normal data structure like list, then CPU have to pre-load whole data before actual using it and then proceed further, instead we can use generator / iterator object which yields the value at runtime once anyone demanded.


๐Ÿ˜ท M : Nice ! but can you create this

image

๐Ÿ˜Ž C : Are you kidding me ! It is so simple


allSum = [i for i in range(0, 7) for j in range (0, i)]
print(allSum)
Enter fullscreen mode Exit fullscreen mode

๐Ÿค— M : Kinda cool ! I declare you are not NOOB Now


๐Ÿš€ Final Thoughts :

This are some comprehensions which anyone can use in their day to day programming usage.

This makes our makes our code more readable and more concise. Although, it is every individual's choice about preferring or not preferring the comprehensions. Here, I am putting my simple try for the comprehension.

Thanks for Reading ๐Ÿ™๐Ÿ™

Discussion (0)

Forem Open with the Forem app