Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7081937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:59:01+00:00 2026-05-28T06:59:01+00:00

I was just playing with list comprehensions and came across this : h =

  • 0

I was just playing with list comprehensions and came across this :

h = [ b for b in range(1, 9) for k in range(b, b*10) if k%2==0 for j in range(2*k, k*k)]

Expected Result:

h = [1, 2, 3, 4, 5, 6, 7, 8]

Actual Result is not as expected, contains len(h) = 196000 items.

Please explain how this works ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:59:02+00:00Added an answer on May 28, 2026 at 6:59 am

    Why do you think this is the expected result?

    Your code is equivalent to:

    h = []
    for b in range(1, 9):
        for k in range(b, b*10):
            if k%2==0:
                for j in range(2*k, k*k):
                    h.append(b)
    

    So, for each number from 1 to 8, it will append it many times to the list

    You can also see how many times each number is added with the help of groupby:

    >>> for i,j in itertools.groupby(h):
        print(i, sum(1 for i in j))
    
    1 80
    2 960
    3 3640
    4 9120
    5 18392
    6 32472
    7 52328
    8 79008
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Was just playing around with nodejs and chrome's console when I tested this: []
I've just started playing around with android, so pardon me if this is a
Just a little idea I'm playing with, not sure if it's viable or has
I'm just playing with some linq and asp.net - absolute beginner so I'm not
I have a sortable list of playing cards. In order to just show the
I've just started playing with GHCi. I see that list generators basically solve an
Hey guys, I'm just playing around with SQLite so this is new for me.
just have a general question that came up while I was playing around with
Just playing around with Wordpress / Contact Form 7 . Is it possible to
Just playing around with memcache for the first time; here's my code: $memcache =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.