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

  • Home
  • SEARCH
  • 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 8970143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:41:26+00:00 2026-06-15T17:41:26+00:00

Hi I have a python problem whereby I have to count which list elements

  • 0

Hi I have a python problem whereby I have to count which list elements contain the value 2, in a list with various levels of nest. E.g.:

my_list = [[2,3,2,2], [[2,1,2,1], [2,1,1]], [1,1,1]]

this list can have have up to three levels of nesting but could also be two or only one level deep.

I have piece of code which sort of works:

count = 0
for i in my_list:
    if len(i) > 1:
        for x in i:
            if 2 in x:
                count += 1
    elif i == 2:
        count += 1

However, apart from being very ugly, this does not take account of the potential to have a list with a single element which is 2. It also doesn’t work to get len() on a single int.

I know list comprehension should be able to take care of this but I am a litle stuck on how to deal with the potential nesting.

Any help would be much appreciated.

  • 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-06-15T17:41:27+00:00Added an answer on June 15, 2026 at 5:41 pm

    I’d use a variant of the flatten() generator from https://stackoverflow.com/a/2158532/367273

    The original yields every elements from an arbitrarily nested and irregularly shaped structure of iterables. My variant (below) yields the innermost iterables instead of yielding the scalars.

    from collections import Iterable
    
    def flatten(l):
        for el in l:
            if isinstance(el, Iterable) and any(isinstance(subel, Iterable) for subel in el):
                for sub in flatten(el):
                    yield sub
            else:
                yield el
    
    my_list = [[2,3,2,2], [[2,1,2,1], [2,1,1]], [1,1,1]]
    print(sum(1 for el in flatten(my_list) if 2 in el))
    

    For your example it prints 3.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have problem with python multithreaded Queues. I have this script, where producer take
I have a very frustrating python problem. In this code fixedKeyStringInAVar = SomeKey def
The problem I have is that I have this Python script to launch a
I have 2 Python scripts which are main_menu.py and inputip.py. The problem occurs when
Basically I have the inverse of this problem: Python Time Seconds to h:m:s I
I have heard of Python's GIL problem, which states that there can only be
I have various python function which i intend to run independently. For exmaple ,
i have this problem: Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56) [GCC 4.4.3] on
I have this problem: System A runs Ubuntu and needs Python 2.6 for a
I have problem concerning python packages and testing. I'm writing an application using wx

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.