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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:49:24+00:00 2026-05-16T11:49:24+00:00

I have a data structure like this: items = [ [‘Schools’, ”, ”, ’32’],

  • 0

I have a data structure like this:

items = [
    ['Schools', '', '', '32'],
    ['Schools', 'Primary schools', '', '16'],
    ['Schools', 'Secondary schools', '', '16'],
    ['Schools', 'Secondary schools', 'Special ed', '8'],
    ['Schools', 'Secondary schools', 'Non-special ed', '8'],
]

It’s a list of spending items. Some are aggregates, e.g. items[0] is aggregate spending on all schools, and items[2] is aggregate spending on secondary schools. Those that are not aggregates are items[1], items[3] and items[4].

How can I elegantly reduce the list so it only shows non-aggregate items? In pseudocode:

for each item in items
   check if item[1] is blank, if it is
       check if item[0] matches another item’s[0]
       if it does and if that item’s[1] isn’t blank
           delete item
   check if item[2] is blank, if it is
       check if item[1] matches another item’s[1]
       if it does and if if that item’s[2] isn’t blank
           delete item 

Here’s my (lame!) attempt so far:

for i in range(len(items)):
    i -= 1
    if items[i]:
        if items[i][1] == "":
            for other_item in items:
                if items[i][0]==other_item[0] and other_item[1]!="":
                    items_to_remove.append(i)
                    continue
        elif items[i][2]=="":
            for other_item in items:
                if items[i][1] == other_item[1] and other_item[2] != "":
                    items_to_remove.append(i)
                    continue
new_items = [ key for key,_ in groupby(items_to_remove)]
new_items.sort(reverse=True)  
for number in new_items:
    temp_item = items[number]
    items.remove(temp_item)

This is just so ugly. What can I do better?

NB: I could use dictionaries instead of lists, if that would make life easier 🙂

  • 1 1 Answer
  • 2 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-16T11:49:25+00:00Added an answer on May 16, 2026 at 11:49 am
    list_keys = [ "".join(x[:-1]) for x in items ]
    for i in range(len(list_keys)-1):
      if not list_keys[i+1].startswith(list_keys[i]):
         print items[i]
    print items[-1]
    

    Here I find the “key” of each item, which is all entries in an item, concatenated, except the last value.

    An aggregate item’s key is always a prefix of succeeding items’ keys, so we can use this test to detect aggregate items and dismiss them.

    This alg. prints (on your input):

    ['Schools', 'Primary schools', '', '16']
    ['Schools', 'Secondary schools', 'Special ed', '4']
    ['Schools', 'Secondary schools', 'Non-special ed', '4'],
    

    Note:
    This assumes all items are ordered neatly in a tree structure (as your original data). If it’s not, it’ll be (slightly) more complicated as you’ll have to sort the keys before the loop (and keep track of which key belongs to which item).

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

Sidebar

Related Questions

I have a data.table object like this one library(data.table) a <- structure(list(PERMNO = c(10006L,
I have four level data structure defined like this: Dictionary<Type1, Dictionary<Type2, Dictionary<Type3, List<Type4>>>> The
My table have data structure like this cate_id task_id date_start date_end other 34 14
Using EF4 and Linq I have a data structure which looks like this: parentid
I have a set of data that is structured like this: ItemA.GroupA ItemB.GroupA ItemC.GroupB
I'm a bit confused on this. I have a data table structured like this:
I have a JavaScript data structure like the following in my Node.js/Express web app:
I have a JSON-like data structure (which I don't want to change) that is
I have the need to use a Stack-like data structure for a program that
I have something like the following data structure: Category StartDateTime EndDateTime =============================================== 1 12/1/2009

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.