Close Menu
Soshace Digital Blog

    Subscribe to Updates

    Get The Latest News, Updates, And Amazing Offers

    What's Hot
    Programming

    16. Уроки Node.js. Событийный цикл, библиотека libUV. Часть 2.

    Programming

    Programming Patterns. Facade, Adapter, Decorator

    Beginners

    Upload Multiple Images to a Django Model without plugins

    Important Pages:
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Today's Picks:
    • Scaling Success: Monitoring Indexation of Programmatic SEO Content
    • Leveraging Influencers: Key Drivers in New Product Launches
    • How Privacy-First Marketing Will Transform the Industry Landscape
    • The Impact of Social Proof on Thought Leadership Marketing
    • Balancing Value-Driven Content and Promotional Messaging Strategies
    • Top Influencer Marketing Platforms to Explore in 2025
    • Emerging Trends in Marketing Automation and AI Tools for 2023
    • Strategies to Mitigate Duplicate Content in Programmatic SEO
    Wednesday, September 10
    Facebook X (Twitter) Instagram LinkedIn YouTube
    Soshace Digital Blog
    • Home
    • About
    • Services
    • Contact Us
    • Privacy Policy
    • Terms & Conditions
    Services
    • SaaS & Tech

      Maximizing Efficiency: How SaaS Lowers IT Infrastructure Costs

      August 27, 2025

      Navigating Tomorrow: Innovations Shaping the Future of SaaS

      August 27, 2025

      Maximizing Impact: Strategies for SaaS & Technology Marketing

      August 27, 2025
    • AI & Automation

      Enhancing Customer Feedback Analysis Through AI Innovations

      August 27, 2025

      Navigating the Impact of AI on SEO and Search Rankings

      August 27, 2025

      5 Automation Hacks Every Home Service Business Needs to Know

      May 3, 2025
    • Finance & Fintech

      Critical Missteps in Finance Marketing: What to Avoid

      August 27, 2025

      Analyzing Future Fintech Marketing Trends: Insights Ahead

      August 27, 2025

      Navigating the Complex Landscape of Finance and Fintech Marketing

      August 27, 2025
    • Legal & Compliance

      Exploring Thought Leadership’s Impact on Legal Marketing

      August 27, 2025

      Maximizing LinkedIn: Strategies for Legal and Compliance Marketing

      August 27, 2025

      Why Transparency Matters in Legal Advertising Practices

      August 27, 2025
    • Medical Marketing

      Enhancing Online Reputation Management in Hospitals: A Guide

      August 27, 2025

      Analyzing Emerging Trends in Health and Medical Marketing

      August 27, 2025

      Exploring Innovative Content Ideas for Wellness Blogs and Clinics

      August 27, 2025
    • E-commerce & Retail

      Strategic Seasonal Campaign Concepts for Online and Retail Markets

      August 27, 2025

      Emerging Trends in E-commerce and Retail Marketing Strategies

      August 27, 2025

      Maximizing Revenue: The Advantages of Affiliate Marketing for E-Commerce

      August 27, 2025
    • Influencer & Community

      Leveraging Influencers: Key Drivers in New Product Launches

      August 27, 2025

      Top Influencer Marketing Platforms to Explore in 2025

      August 27, 2025

      Key Strategies for Successful Influencer Partnership Negotiations

      August 27, 2025
    • Content & Leadership

      The Impact of Social Proof on Thought Leadership Marketing

      August 27, 2025

      Balancing Value-Driven Content and Promotional Messaging Strategies

      August 27, 2025

      Analyzing Storytelling’s Impact on Content Marketing Effectiveness

      August 27, 2025
    • SEO & Analytics

      Scaling Success: Monitoring Indexation of Programmatic SEO Content

      August 27, 2025

      Strategies to Mitigate Duplicate Content in Programmatic SEO

      August 27, 2025

      Effective Data Visualization Techniques for SEO Reporting

      August 27, 2025
    • Marketing Trends

      How Privacy-First Marketing Will Transform the Industry Landscape

      August 27, 2025

      Emerging Trends in Marketing Automation and AI Tools for 2023

      August 27, 2025

      Maximizing ROI: Key Trends in Paid Social Advertising

      August 27, 2025
    Soshace Digital Blog
    Blog / Python / Comprehension in Python
    Programming

    Comprehension in Python

    rrajcorpBy rrajcorpFebruary 5, 2020Updated:February 6, 2020No Comments9 Mins Read
    Facebook Twitter Pinterest Telegram LinkedIn Tumblr Email Reddit
    Comprehension in Python
    Comprehension in Python
    Share
    Facebook Twitter LinkedIn Pinterest Email Copy Link
    Comprehension in Python
    Comprehension in Python

    Python is well known for its simplicity, readability, and making applications with the fewest lines of code possible. Comprehension is one of Python’s key features that not only targets code simplicity but also better performance. It is a feature of Python by which sequences are constructed from another sequence. It’s critical for us, as Python developers, to understand how comprehensions work. In this article, we will discuss Python Comprehensions and how to optimize your code.

    Now, have you seen comprehension anywhere else? Is it something unique to Python?

    no, it's not meme
    no, it’s not meme

    It is not a new concept as it already had its presence in functional programming (Haskell, Scala, etc). However, it is very rare to find this feature outside of functional programming paradigm.

    What is Comprehension?

    We have seen some of the definitions of comprehension in Python and its benefits. We saw that we can generate iterable objects in a single expression using comprehensions without regular for loops. But what exactly is a comprehension? How exactly the respective Python implementation looks? To explain them better, let’s talk about different types of comprehensions in Python. Currently, there are four different types of comprehensions supported in Python.

    1. List comprehensions
    2. Set comprehensions
    3. Dictionary comprehensions
    4. Generator comprehensions

    Let’s observe what it looks like to implement comprehension for list creation. We’re using a normal for loop below to iterate through an array and add values in it:

    array=[] 
    for i in range(1000):
        array.append(i**2)

    Below, is the same task done using list comprehension:

    array=[i**2 for i in range(1000)]
    wow, that's awesome meme
    wow, that’s awesome meme

    We effectively reduced the task to a one-liner! Don’t worry about what the above code does for now. The intention was to create an idea of what comprehension can do. We will discuss different types of comprehension in the upcoming sections.

    Pre-requisites

    This article assumes that you have basic programming knowledge in Python and an understanding of data structures in Python. You need to have Python installed on your system.  List comprehension is introduced in Python 2. Set/Dict comprehensions are introduced in Python 3. So, you need to have Python 3 installed on your system in order to try out all the comprehension examples explained in this article.

    Why Comprehensions?

    One of the benefits of comprehensions is that it will allow us to write less code that is easier to understand. Although programmers from procedural programming background may disagree on this. Programmers who come from a C++/Java background may question the readability of comprehensions. Comprehensions follow the functional programming approach. There’s also performance advantage for comprehensions. It can be proved by calling timeit() method from the timeit package. For example, let us create an array by looping into numbers from 0 to 1000. Below are the two different approaches: One with regular for loop and the other one with comprehensions:

    #Use timeit to measure the time required to execute the block of code
    import timeit
    
    code1= """
    array=[]
    for i in range(1000):
         array.append(i**2)
    """
    print(timeit.timeit(code1,number=1))
    
    code2="""
    array=[i**2 for i in range(1000)]
    """
    print(timeit.timeit(code2,number=1))

    Let’s observe the execution time (measured in seconds) for these two code blocks as shown in below snapshot:

    Speed snapshot
    Speed snapshot

    Here we are measuring the time required to execute the code blocks exactly once. The regular for loop (“code1”) takes around 1.3 milliseconds, but the same task with comprehension (“code2”) is accomplished in 1.1 milliseconds.  That’s just a matter of 0.2 milliseconds, but it makes sense for a complex application with millions of such computations. Thus comprehension achieves both simplicity and performance at the same time.

    Read More:  Vagrant Tutorial #part 2

    List Comprehensions

    Now that we have had enough introduction to comprehension, let’s examine how to implement a list comprehensions. Let us take an example where you need to print all prime numbers till number ‘n’. Below is a sample method to check whether the given number is prime:

    def isPrime(number):
        for i in range(2,number):
            if number%i == 0:
                return False
        return True

    Now, let’s say we need to print all prime numbers that are under n=1000. We can use a list comprehension to create a list of all prime numbers as shown below:

    primes = [i for i  in range(1,1000) if isPrime(i)]
    print(primes)
    Prime numbers: output
    Prime numbers: output

    And the output is:

    Here, we have only the ‘if’ conditional block, so the actual linking is happening as shown below. Comprehensions start with expressions (‘i’ in the below example).

    Linking
    Linking

    If you need to include both ‘if‘ and ‘else‘ conditional blocks in your comprehension, then you can re-write the code in the below manner. In the below code, we have replaced all non-prime numbers with zero for the demonstration purpose.

    primes = [i if isPrime(i) else 0 for i  in range(1,1000) ]

    At this point, you can think of a list comprehension as a replacement for inbuilt methods such as map(), filter() and lambda functions.  We can generate new lists,  iterate through existing lists, and perform flattening on multi-dimensional lists using list comprehension.

    Set Comprehensions

    A set comprehension is no different from a list comprehension except that it creates a set instead of a list. Let’s take the same example discussed earlier and generate a set using a set comprehension.

    primes = {i for i  in range(1,1000) if isPrime(i)}
    print(primes)
    Output of a set comprehension
    Output of a set comprehension

    And the output is:

    At this moment, you would feel like…

    not bad meme
    not bad meme

    Well, we haven’t reached the best part yet. Stay tuned till the end!

    Dictionary Comprehensions

    Just like we created lists/sets using comprehension, we can also create dictionaries using dictionary comprehensions. Let us consider the below example. We have a list of students and a mark list indicating the scores for each student.

    students = ['student1','student2','student3','student4']
    marks = [45, 78, 12, 14]

    So, our target is to create a dictionary mapping student scores to respective students. The regular approach would be to loop over the list length (student/marks) followed by assigning key values:

    size = len(students)
    mapping={}
    for i in range(size):
        mapping[students[i]]=marks[i]

    The same can be achieved using a dictionary comprehension as shown below:

    mapping = {students[i]:marks[i] for i in range(len(students))}

    Here is what you will see while printing them.

    Output
    Output

    Now let’s say we want to create a dictionary with students who scored more than 40. We can achieve it using a dictionary comprehension as shown below:

    students_after_filter = {k:v for k,v in mapping.items() if v>40}

    Note that mapping.items() will return a list of iterable key-value pairs.

    So, now you should see the below output:

    Dictionary comprehension output
    Dictionary comprehension output

    Generator Comprehensions

    Generator comprehensions are similar to the list/set comprehensions, the only difference is that we use circular brackets in a generator comprehension. The motive behind the introduction of a generator comprehension in Python is to have a memory-efficient approach in place. In a generator comprehension, memory is allotted on the go and not on the startup. In the list/set comprehensions, we generate the entire elements first and keep them in memory. However, a generator comprehension employs a lazy-loading approach. It is worth talking a bit about a generator before we jump into the discussion of a generator comprehension.  A generator is a function with a yield statement instead of returning a value. The peculiarity of a yield statement is that we can pause the method at any time and resume it back at a later point if needed. Let’s create a generator method to generate infinite prime numbers on demand.

    import itertools
    
    def genPrime():
        for i in itertools.count(1):
            if isPrime(i):
                yield i

    We have imported an itertools package for a specific purpose. Note that, itertools.count() creates an iterable object with a capability of holding infinite series of elements. The idea is to generate a series of elements until a condition is met without mentioning any upper bound.

    itertools.count()-> 0,1,2,3,4,5,...
    itertools.count(1) -> 1,2,3,4,5,6,...
    itertools.count(1, 2) -> 1,3,5,7,9,...

    Let’s create another method to print out prime numbers:

    def print_prime_numbers(f,n):
        for prime,_ in zip(f, range(n)):
            print(prime)

    We can print prime numbers under the given range as shown below (n=10 in the below example):

    print_prime_numbers(genPrime(),10)
    Output
    Output

    Note that we have two methods here: one to generate infinite prime numbers and the other one to print the prime numbers. Now, what if we don’t have to create those methods with yield statements? Yes, that’s where a generator comprehension comes into play. We can skip creating methods with a yield keyword by using a generator comprehension. Generator method discussed earlier can be converted to a one-liner as shown below:

    prime_numbers = (i for i  in itertools.count(1) if isPrime(i))

    Print the prime numbers to verify the results:

    print_prime_numbers(prime_numbers,10)
    Output
    Output

    In a nutshell, a generator comprehension deals with only one item at a time and only when there’s a demand. For the same reason, a generator comprehension is memory-efficient in comparison to a list/set comprehension. We can prove this with a timeit package.

    import timeit
    code1="""
    numbers = (i for i  in range(1,1000))
    """
    
    code2="""
    numbers = [i for i  in range(1,1000)]
    """
    
    print(timeit.timeit(code1,number=1000))
    print(timeit.timeit(code2,number=1000))

    We observe below execution time (measured in seconds and for 1000 executions) for the above-mentioned code blocks:

    Read More:  5 Best JavaScript Conferences to Attend in 2019
    Execution time
    Execution time

    As you can see, that’s a significant difference between these two, which indicates the efficiency of a generator comprehension.

    aren't we all?
    aren’t we all?

    But again, it depends on the requirements. If you prefer performance and only need to iterate once through the objects, then simply use a generator comprehension. If you need to store iterable objects and need to iterate through it multiple times, then better off with a list/set comprehension. And with this, we have discussed all the four comprehension types in Python.

    We have discussed very basic examples of comprehension types in Python. For all those procedural programmers out there who are learning Python, the first thing you might want to practice is to write comprehensions instead of regular loop constructs. You may start with a regular procedural approach first and then convert it to use comprehension. Eventually, you will get addicted to the comfort! So, that’s it for now and hope you find this article useful.

    python
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    rrajcorp

      Related Posts

      Mastering REST APIs: Essential Techniques for Programmers

      December 18, 2024

      Crafting Interactive User Interfaces Using JavaScript Techniques

      December 17, 2024

      Effective Strategies for Utilizing Frameworks in Web Development

      December 16, 2024
      Leave A Reply Cancel Reply

      You must be logged in to post a comment.

      Stay In Touch
      • Facebook
      • Twitter
      • Pinterest
      • Instagram
      • YouTube
      • Vimeo
      Don't Miss
      Python February 26, 2020

      Creating Real-Time API with Beautiful Soup and Django REST Framework

      In this post, we will use Beautiful Soup and Django REST Framework to create real-time API by crawling data. At the end of this tutorial, you’ll be able to turn any website into an API.

      Progressive Web Applications and Service Workers

      December 17, 2019

      Facilisi Nullam Vehicula Ipsum Arcu Cursus Vitae Congue

      January 28, 2020

      Yarn vs. npm in 2019: Choosing the Right Package Manager for the Job

      June 11, 2019

      Categories

      • AI & Automation
      • Angular
      • ASP.NET
      • AWS
      • B2B Leads
      • Beginners
      • Blogs
      • Business Growth
      • Case Studies
      • Comics
      • Consultation
      • Content & Leadership
      • CSS
      • Development
      • Django
      • E-commerce & Retail
      • Entrepreneurs
      • Entrepreneurship
      • Events
      • Express.js
      • Facebook Ads
      • Finance & Fintech
      • Flask
      • Flutter
      • Franchising
      • Funnel Strategy
      • Git
      • GraphQL
      • Home Services Marketing
      • Influencer & Community
      • Interview
      • Java
      • Java Spring
      • JavaScript
      • Job
      • Laravel
      • Lead Generation
      • Legal & Compliance
      • LinkedIn
      • Machine Learning
      • Marketing Trends
      • Medical Marketing
      • MSP Lead Generation
      • MSP Marketing
      • NestJS
      • Next.js
      • Node.js
      • Node.js Lessons
      • Paid Advertising
      • PHP
      • Podcasts
      • POS Tutorial
      • Programming
      • Programming
      • Python
      • React
      • React Lessons
      • React Native
      • React Native Lessons
      • Recruitment
      • Remote Job
      • SaaS & Tech
      • SEO & Analytics
      • Soshace
      • Startups
      • Swarm Intelligence
      • Tips
      • Trends
      • Vue
      • Wiki
      • WordPress
      Top Posts

      How We are Looking for Proposals on UpWork

      Remote Job January 8, 2016

      Conquering Imposter Syndrome: Empowering Startup Founders

      Startups December 16, 2024

      Navigating International Hiring: Solutions for Recruitment Challenges

      Recruitment November 26, 2024

      RxJS Methods. Part 1

      Programming April 14, 2017

      Subscribe to Updates

      Get The Latest News, Updates, And Amazing Offers

      About Us
      About Us

      Soshace Digital delivers comprehensive web design and development solutions tailored to your business objectives. Your website will be meticulously designed and developed by our team of seasoned professionals, who combine creative expertise with technical excellence to transform your vision into a high-impact, user-centric digital experience that elevates your brand and drives measurable results.

      7901 4th St N, Suite 28690
      Saint Petersburg, FL 33702-4305
      Phone: 1(877)SOSHACE

      Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
      Our Picks
      Git

      Introduction to GitHub Desktop: A GUI Enhancement to a CLI Approach

      JavaScript

      TOP 5 Latest Books for Hiring Best People in Tech (and Other Areas)

      Startups

      Strategic Approaches to Securing Startup Funding Successfully

      Most Popular

      Mapping the World: Creating Beautiful Maps and Populating them with Data using D3.js 

      JavaScript

      Strategic Approaches to Navigating Entrepreneurship Competition

      Entrepreneurship

      The Era of Change – IoT and Machine Learning | Trends in Industry for 2020

      Trends
      © 2025 Soshace Digital.
      • Home
      • About
      • Services
      • Contact Us
      • Privacy Policy
      • Terms & Conditions

      Type above and press Enter to search. Press Esc to cancel.