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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:44:04+00:00 2026-06-15T18:44:04+00:00

I have a heap (python, heapq module) like this – >>> h = []

  • 0

I have a heap (python, heapq module) like this –

>>> h = []
>>> heappush(h, (5, 'write code'))
>>> heappush(h, (7, 'release product'))
>>> heappush(h, (1, 'write spec'))
>>> heappush(h, (3, 'create tests'))

How do I remove the tuple with item value as “create tests” in O(logn) and preserve the heap property?

This is what I could come up with (not O(logn))

for i in range(len(h)):
   if h[i][1] == "create tests":
      h[i], h[-1] = h[-1], h[i]
      popped = h.pop()
      heapq.heapify(h)
      break
  • 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-15T18:44:05+00:00Added an answer on June 15, 2026 at 6:44 pm

    I’m afraid there’s no such method with heapq only. Since searching an element from a heap requires O(n).

    But you can use it together with something like dict, which gives O(1) time for searching an entry.

    UPDATED:

    I tried using a dict for bookkeeping, but how can I get the index of “create test” when it was inserted? – Prakhar 3 hours ago

    A naive approach would be:

    # remember to update this hdict when updating the heap.
    hdict = { h[i][1]: i for i in range(len(h)) }
    

    Then you can just obtain index of a given string by accessing this hdict instead of your O(n) linear search.

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

Sidebar

Related Questions

Python has heapq module which implements heap data structure and it supports some basic
I have a vector that I want to use to create a heap. I'm
I have written the following heap sort code and I get the wrong output
Suppose I have a Heap Like the following: 77 / \ / \ 50
I have a Telit module which runs [Python 1.5.2+] (http://www.roundsolutions.com/techdocs/python/Easy_Script_Python_r13.pdf)!. There are certain restrictions
When I compile in release mode, I have heap corruption on the deallocation of
This question is related to this one : If I have a heap dump
I have a big xml document that looks like this: <Node name=foo> <Node name=16764764625>
The collections.Count.most_common function in Python uses the heapq module to return the count of
I have a heap (implemented like a binary tree: each node has two pointers

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.