pferate
pferate

Reputation: 2107

Do Dictionaries have a key length limit?

I was wondering if Python had a limit on the length of a dictionary key.

For clarification, I'm not talking about the number of keys, but the length of each individual key. I'm going to be building my dictionaries based on dynamic values (after validation), but I'm not sure if I should be taking length into account in this case.

Upvotes: 33

Views: 34085

Answers (3)

Hugh Bothwell
Hugh Bothwell

Reputation: 56644

Here's a bit of sample code:

from string import ascii_letters
from random import choice

def make_str(length):
    return "".join(choice(ascii_letters) for i in range(length))

test_dict = {make_str(10000000): i for i in range(5)}

Conclusion: Python will quite happily use a 10-million-character string as a dict key.

Upvotes: 26

Brian
Brian

Reputation: 3131

There is no such limit in place regarding dictionary keys. Since python also has arbitrary precision on numeric types, the only limit you will encounter, string or otherwise, is that of available memory. You can see another post here for a discussion on maximum string length in python 2.

Upvotes: 26

AlexF
AlexF

Reputation: 497

As I know there's no limit but consider that more the key is long, the more the time to create/access the keys

Upvotes: 2

Related Questions