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 8351731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:44:05+00:00 2026-06-09T08:44:05+00:00

This is probably quite a straightforward question, but I can’t find an answer elsewhere

  • 0

This is probably quite a straightforward question, but I can’t find an answer elsewhere so I’ll ask.
What is the best way to find the number of times an element appears in a nested list?
For example:

my_list=[[a,b,c,d],[a,b,z,d],[a,c,f,e],[d,w,f,a]]

How would I find how many times ‘a’ is the first element of the list? Or more generally, how many times ‘a’ appears in my_list at all? I imagine there’s a way to do this with collections.Counter, but I haven’t been able to figure it out.

EDIT
For my_list, I would like an output of a:3 when counting if it’s the first element of the list. If the question was changed to see if b is the second element, the desired output would be b:2

  • 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-09T08:44:06+00:00Added an answer on June 9, 2026 at 8:44 am

    Use a nested generator expression:

    Counter(x for sublist in my_list for x in sublist)
    

    To count the items in the first position, a different generator expression gets that item for counting:

    Counter(sublist[0] for sublist in my_list)
    

    Demo:

    >>> from collections import Counter
    >>> my_list=[['a','b','c','d'],['a','b','z','d'],['a','c','f','e'],['d','w','f','a']]
    >>> Counter(x for sublist in my_list for x in sublist)
    Counter({'a': 4, 'd': 3, 'c': 2, 'b': 2, 'f': 2, 'e': 1, 'w': 1, 'z': 1})
    >>> Counter(sublist[0] for sublist in my_list)
    Counter({'a': 3, 'd': 1})
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably quite simple but I've googled and can't find an answer. I
This question is probably quite dumb, but I can't find an example and can't
I know this is probably quite straightforward but cannot seem to find an example
This is probably quite a simple question, but I can't remember how to do
This is probably a very obvious question but I can't quite figure it out.
This is probably quite simple but I can't seem to find it readily. In
This is probably quite a newb question, but I can't seem to figure it
This is probably a really simple question but one I've never quite worked out
This probably is a dummy question but I cannot find a clear indication. I
This is probably quite straight-forward but at the moment I can't think of a

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.