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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:31:12+00:00 2026-05-29T10:31:12+00:00

In Python, is there any counter available during the list comprehension as it would

  • 0

In Python, is there any counter available during the list comprehension as it would be in case of a for loop?

It would be more clear why I need a counter, with this example:

I wish to achieve the following:

Initial List: ['p', 'q', 'r', 's']

Desired List: [(1, 'P'), (2, 'Q'), (3, 'R'), (4, 'S')]

In the desired list, first element of every tuple are ordinal numbers. If it were just flat list, I could have used zip to achieve this. But however, the list I am dealing with is nested, three level deep (think of hierarchical data), and it is generated through list comprehension.

So, I was wondering is there any way to introduce those ordinal numbers during list comprehension. If not, what would be the best possible solution.

P.S. : Here the lower case letters are converted to uppercase, but that is not a part of problem, think of it as just a data conversion.

Code:

allObj = Category.objects.all()

tree =[(_, l1.name, [(__, l2.name, [(___, l3.name) for l3 in allObj if l3.parentid == l2.categoryid]) for l2 in allObj if l2.parentid == l1.categoryid]) for l1 in allObj if l1.parentid == None]

allObj contains data from table category, which in turn contains hierarchical data represented in the form of Adjacency List.

I have put _ where I need ordinal numbers to be. Notice that the list is nested, so there will be a separate counter at each level represented by 1, 2 & 3 _s.

  • 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-29T10:31:13+00:00Added an answer on May 29, 2026 at 10:31 am

    The most basic case

    [(i, x) for i, x in enumerate(some_list, 1)]
    

    Apply a filter with an if-statements

    [(i, x) for i, x in enumerate(some_list, 1) if i > 2]
    

    or like this

    [(i, x) for i, x in enumerate(some_list, 1) if x != 'p']
    

    A word of advice

    Most often you don’t need to do this. Instead you just call enumerate(some_list, 1) where the enumeration is needed, in a for loop for example.

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

Sidebar

Related Questions

Is there any python library to get a list of countries for a specific
Is there any way to read out the timestamp-counter on x86 CPUs in Python?
Is there any Python project/module working on a binary plist writer? I need one
Is there a way to access an iteration-counter in Python's for loop? I usually
In Python is there any way to make a class, then make a second
Is there any python module to convert PDF files into text? I tried one
Are there any python libraries for data trees? I mean a tree as a
Is there any way to get Python to use my ActiveTcl installation instead of
Is there any Ruby equivalent for Python's builtin zip function? If not, what is
Is there any GUI toolkit for Python with form designer similar to Delphi, eg

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.