I want to test if a value is a key from a dictionary, i do this like so:

if value in dict:
     print "value found"
else:
     print "value not found"

Now this doesn't work if value = "abcd" and the key in the dictionary is "Abcd"...

How do i say he has to compare the values but uppercase and lowercase is the same?

(i don't want to lowercase all the words in my dictionary as we are talking a 200Mb dictionary here)

Dani AI

Generated

Short answer: do a case-insensitive lookup by normalizing the lookup string, but avoid blindly lowercasing the entire 200MB structure if you need to preserve original-case keys or avoid extra memory.

A few quick notes on earlier suggestions: was right that in checks keys (not values). ’s capitalize() is brittle (it only changes the first character). ’s "string-of-keys" trick can accidentally match substrings (so "ham" could match inside "shambles") and is fragile for punctuation. correctly raised the real design question: what should happen when the same word appears in multiple case forms? That determines whether you canonicalize or preserve originals.

A practical pattern that keeps data mostly as-is is an auxiliary normalization index: keep your main mapping keyed by the original word form, and maintain a small index that maps the normalized form (use str.casefold() for robust Unicode folding) to a canonical original key (or to a small set of original keys). Lookups normalize the query, find the canonical key(s) via the index, then fetch the actual list(s) of URLs from the main mapping. This avoids copying all values and only stores one small extra string per unique normalized word. Use collections.defaultdict or dict.setdefault to simplify building lists. See str.casefold() and collections.defaultdict in the Python docs for normalization and helper structures (casefold, defaultdict).

If memory is still a concern, consider an on-disk store (SQLite with a NOCASE column or a key-value store) so you can index casefolded keys without keeping the whole dataset in RAM.

Recommended Answers

All 8 Replies

Member Avatar for Member #750452
if value or value.capitalize() in mydict:

is this what you need?
greetings,
CobRalf

No you need this:

if value in myDict.values():
    print("value in dictionary")
else:
    print("value not in dictionary")

The brute force approach is maybe not so bad since you don't have to lower the entire dictionary, just the string of its keys.

>>> d = {'Spam': 2, 'Ham': 1, 'Eggs': 3} #Here is a dictionary
>>> s = str(d.keys()) #Here is a string of its keys
>>> 'ham' in s.lower()
True
>>>

The brute force approach is maybe not so bad since you don't have to lower the entire dictionary, just the string of its keys.

>>> d = {'Spam': 2, 'Ham': 1, 'Eggs': 3} #Here is a dictionary
>>> s = str(d.keys()) #Here is a string of its keys
>>> 'ham' in s.lower()
True
>>>

Hmm, i had not thought of that and it is true and makes sense.
I'm going to do it like you said and just lower all the keys,

thanks!

A dictionary consists of key:value pairs. The keys have to be unique. If you had for instance this dictionary:

d = {'abc': 1, 'Abc': 2, 'abC': 3}

Now suddenly you set the key to all lower case, which associated value to you really want?

A dictionary consists of key:value pairs. The keys have to be unique. If you had for instance this dictionary:

d = {'abc': 1, 'Abc': 2, 'abC': 3}

Now suddenly you set the key to all lower case, which associated value to you really want?

I have a dictionary like this:


Word - Web documents it is found on

google - http://www.google.com
IT - http://www.daniweb.com
Discussion - http://www.daniweb.com | http://www.wikipedia.com
etc. etc.

Before adding a word to the dictionary it is checked if it's already there, if it is the list of links is retrieved and appended and if it's not in there it just gets added:

if word in dict:
		list = dict[word]
		list.append(link)
	else :
		dict[word] = [link]

I don't think we are talking about the same thing. Python has an container object called a dictionary.

I don't think we are talking about the same thing. Python has an container object called a dictionary.

Yes i know, uhm lets see how i can explain how i'm doing this.
Assume we have 3 web pages:

Webpage 1:

This is a String

Webpage 2:

Also a string

Webpage 3:

The last sTriNg

dict = {}
for webpage in webpages:
    allwords = re.findall([\w\d]+)
    for word in allwords:
        key = word.lower()
        if key in dict :
            list = dict[key]
            list.append(webpage)
        else:
            dict[key] = [webpage]

So in the first iteration we have to index:
- This
- is
- a
- String

Since there is nothing in the dictionary they will all we be added and the dictionary looks like this (all words are lowered):

[this] = webpage1
[is] = webpage1
[a] = webpage1
[string]= webpage1

In the next interation we have to index: Also, a and string

"Also" is not in the dictionary so it just get's added:

[also] = webpage2

"a" is already in the dictionary so the list is being appended for 'a':

[a] = [webpage1, webpage2]

Same goes for string and the dictionary looks like this:

[this] = webpage1
[is] = webpage1
[a] = [webpage1,webpage2]
[string]= [webpage1,webpage2]
[also] = [webpage2]

Repeat for last webpage and the dictionary looks like this:

[this] = webpage1
[is] = webpage1
[a] = [webpage1,webpage2]
[string]= [webpage1,webpage2,webpage3]
[also] = [webpage2]
[the] = webpage3
[last] = webpage3


I hope this makes sense and the code is on top of my head because my situation is somewhat more complex, so this snippet could contain errors. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.