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)

Member Avatar for CobRalf
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.