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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:25:44+00:00 2026-05-31T19:25:44+00:00

I have Python nested list that I’m trying to organize and eventually count number

  • 0

I have Python nested list that I’m trying to organize and eventually count number of occurrences. The nested list looks like:

[['22', '1'], ['21', '15'], ['11', '3'], ['31', '4'], ['41', '13'],...]

The first I want to do is create a sublist that only contains ‘1’ corresponding to the second item in the nested list. I was able to do this by the following command:

Subbasin_1 = []
Subbasin_1.append([x for x in Subbasins_Imp if x[1] == '1'])
print Subbasin_1

Giving these results, which are correct:

[['21', '1'], ['21', '1'], ['21', '1'], ['21', '1'], ['22', '1'],...]

Now I want to create another sublist that will give me all the ’21’ in the each nested list for Subbasin_1. When I use the same line of script, but change the appropriate items, I get an empty list. Not sure what is going on…?

OS_Count1 = []
OS_Count1.append([x for x in Subbasin_1 if x[0] == '21'])
print OS_Count1

Result is [[]] ??? What’s the difference between the two?
Thanks for any help…

  • 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-31T19:25:45+00:00Added an answer on May 31, 2026 at 7:25 pm

    I don’t believe that your

    [['21', '1'], ['21', '1'], ['21', '1'], ['21', '1'], ['22', '1'],...]
    

    line could be produced by the code you gave. Your Subbasin_1.append line appends a list to the empty list Subbasin_1, so you should get something like

    [[['22', '1'], ['21', '1']]]
    

    with one extra level of nesting.

    If you avoid the unnecessary construction of an empty list + append, you should get what you want:

    >>> Subbasins_Imp = [['22', '1'], ['21', '15'], ['11', '3'], ['31', '4'], ['41', '13'], ['21', '1']]
    >>> 
    >>> Subbasin_1 = [x for x in Subbasins_Imp if x[1] == '1']
    >>> print Subbasin_1
    [['22', '1'], ['21', '1']]
    >>> OS_Count1 = [x for x in Subbasin_1 if x[0] == '21']
    >>> print OS_Count1
    [['21', '1']]
    

    Alternatively, you could simply replace append by extend. I don’t recommend this, but it might help you to see what’s happening:

    >>> Subbasins_Imp = [['22', '1'], ['21', '15'], ['11', '3'], ['31', '4'], ['41', '13'], ['21', '1']]
    >>> 
    >>> Subbasin_1 = []
    >>> Subbasin_1.extend([x for x in Subbasins_Imp if x[1] == '1'])
    >>> print Subbasin_1
    [['22', '1'], ['21', '1']]
    >>> 
    >>> OS_Count1 = []
    >>> OS_Count1.extend([x for x in Subbasin_1 if x[0] == '21'])
    >>> print OS_Count1
    [['21', '1']]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to remove items from a nested list in Python. I have
Basically I have a nested list that I am trying to sort through the
I have this python code that will traverse a tree structure. I am trying
I'm a python newb and am having trouble groking nested list comprehensions. I'm trying
In Python you can have multiple iterators in a list comprehension, like [(x,y) for
I have a JSON/Python nested list which represents a tree, each item being a
I am learning python and panda3d currently. I have a nested list which i
I have written a Python module that I'd like to make available for others.
beginner to python here. I have 2 nested lists that I want to merge:
I have a list of vectors (in Python) that I want to normalize, while

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.