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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:55:51+00:00 2026-06-05T14:55:51+00:00

I have a list of actions from a database. This is copied into a

  • 0

I have a list of actions from a database. This is copied into a deque since I want to deal with it in order, popping stuff off the left as I go.

So I have something like actions = deque(actions) which is fine.

Each action item is a list from the psycopg module using the DictCursor. Each list has the item ‘phase’.

So things go in phases. Some actions are in phase ‘a’, some in phase ‘b’, etc. Not the best way to store the data but that’s what I was given.

So to make my life easier I want to split the deque into several deques by phase.

So if actions[0][‘phase’] == ‘a’ then this goes in a list containing only items from phase a, and so on with, b, etc.

I could do this with a bunch of ifs and appending but that seems like a lot of effort. I think the answer might be filter(), but I’m not so sure how to use it.

Random stuff to note:

  • Each item is in order, the order needs preserving within each deque.
  • The phases are known and sequential. For example, if phase c doesn’t exist, we know that phase d doesn’t exist. There are a finite number of phases, something like 5 if I recall.

Clarification attempt:

I have a deque, actions. something like:

actions = [
    ['phase': 'a', 'something_else': 'x'], 
    ['phase': 'a', 'something_else': 'y'],
    ['phase': 'b', 'something_else': 'x']
]

Want to end up with (something like):

a = [
    ['phase': 'a', 'something_else': 'x'], 
    ['phase': 'a', 'something_else': 'y']
]
b = [
    ['phase': 'b', 'something_else': 'x']
]

With the minimal amount of code, and something that works for any number of phases/items in phases/etc.

  • 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-05T14:55:53+00:00Added an answer on June 5, 2026 at 2:55 pm

    First define a key function that returns the phase when given an action, e.g.

    key = lambda action: action["phase"]
    

    Now first sort by key — this does not rearrange order more than necessary, i.e. order is conserved for each phase (it’s “stable“) — then use groupby from itertools like this:

    from itertools import groupby
    
    actions.sort(key=key)
    
    results = []    
    for phase, action_iterable in groupby(actions, key=key):
        action_list = list(action_iterable)
        action_list.reverse()
        results.append((phase, action_list)))
    

    As you see I’ve reversed the lists. This is so you can just efficiently pop off the end of the lists instead of using popleft on a deque. If you prefer, turn them into a deque instead of reversing. Now use like this:

    for phase, actions in results:
        while actions:
            action = actions.pop()
            # etc...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have list of structure. I want to modify a particular data from the
I have a list view, the contain is populated from a database. And I
In my viewmodel, I have a list of items I fetch from the database
I have written a code like this to fetch data from database using HQL:
i have a drop down list populated with values from database. What i am
I have a list of divs with onclick actions associated with them. Inside each
Here's my problem: I have do create a menu/list of actions (which would be
I have programmed a shopping list. The footer contains buttons for actions on a
I have controller PlayerController and actions inside: View , Info , List . So
I have a submenu that list departments. Behind this each department have an action

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.