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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:37:49+00:00 2026-06-13T08:37:49+00:00

I have a bytearray that I need to use as a key to a

  • 0

I have a bytearray that I need to use as a key to a dictionary. Ideally I’d like to do this without doing a copy of memory the size of the bytearray. Is there anyway to do this?
Basically,

b = some bytearray
d[byte(b)] = x

Is there any faster way to do this? byte(b) is an O(len(bytearray)) operation which is undesirable.

  • 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-13T08:37:50+00:00Added an answer on June 13, 2026 at 8:37 am

    Any hash algorithm that actually does its job correctly will use O(len(b)) time. So the answer to “is there any faster way to do this” is no.

    If your actual concern is memory usage, then you could, in principle, add a __hash__ method to a subclass of bytearray. But that’s a pretty bad idea. Look what happens:

    >>> class HashableBytearray(bytearray):
    ...     def __hash__(self):
    ...         return hash(str(self))
    ... 
    >>> h = HashableBytearray('abcd')
    >>> hash(h)
    -2835746963027601024
    >>> h[2] = 'z'
    >>> hash(h)
    -2835746963002600949
    

    So the same object could hash to two different spots in the dictionary, which isn’t supposed to happen. And it gets worse:

    >>> d = dict()
    >>> hb1 = HashableBytearray('abcd')
    >>> hb2 = HashableBytearray('abcd')
    >>> d[hb1] = 0
    >>> d[hb2] = 1
    >>> d
    {bytearray(b'abcd'): 1}
    

    Ok, so far, so good. The values are equal, so there should be only one item in the dictionary. Everything is working as expected. Now let’s see what happens when we change hb1:

    >>> hb1[2] = 'z'
    >>> d[hb2] = 2
    >>> d
    {bytearray(b'abzd'): 1, bytearray(b'abcd'): 2}
    

    See how even though hb2 didn’t change at all, it created a new key-value pair in the dictionary this time?

    Every time I passed a key to d, that key was equal to 'abcd'. But because the value of the first key changed after being added to the dictionary, Python couldn’t tell that the value of the new key was the same as the old key had been when it was added. Now there are two key-value pairs in the dictionary, when there should be only one.

    This is only one of many ways that using mutable values as keys can lead to unpredictable and very wrong behavior. Just convert the bytearray to an immutable type, or work with immutable types in the first place.


    And for the inquisitive: sure, buffer caches the first hash, but that doesn’t help at all. There are only two key values, so this should generate only two dict entries:

    >>> a, b, c = bytearray('abcd'), bytearray('abcd'), bytearray('abzd')
    >>> a_buf, b_buf, c_buf = buffer(a), buffer(b), buffer(c)
    >>> d = {b_buf:1, c_buf:2}
    >>> b[2] = 'z'
    >>> d[a_buf] = 0
    

    But it generates three:

    >>> d
    {<read-only buffer for 0x1004a2300, size -1, offset 0 at 0x100499cb0>: 1, 
     <read-only buffer for 0x1004a2420, size -1, offset 0 at 0x100499cf0>: 0, 
     <read-only buffer for 0x1004a22d0, size -1, offset 0 at 0x100499c70>: 2}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SHA-1 byte array that I would like to use in a
I have a bytearray object in Python 2.7 that I got from a query
I have a byte array and I need to convert this to a image.
I have a bunch of RTP packets that I'd like to re-assemble into an
I have the following string that I would like to Huffman-encode and store efficiently
Problem I need a key-value store that can store values of the following form:
I have something like this in my code: String boundary = thisIsMyBoundaryString; StringBuilder body
This might sound odd, but my issue is that I have a text string
I have a byte array that can be of size 2,3 or 4. I
I need to read unmanaged memory into a managed byte array. For this I

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.