Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

NEED HELP TO SEARCH FOR STRING VALUE IN DICTIONARY

I have dictionaries filled with keys of keys. I am trying to create a function to be able to search for if the key has "sg" or "pl".

Here are two examples of two such dictionaries:

Types of conjugations

persons = {1: "1st person sg", 2: "2nd person sg", 3: "3rd person sg", 4: "1st person pl", 5: "2nd person pl", 6: "3rd person pl"};

Future Tense

futureTenseEndings = {persons[1]: "é", persons[2]: "ás", persons[3]: "á", persons[4]: "ámos", persons[5]: "áis", persons[6]: "án"};

Here is some code I found online:

#Just an example how the dictionary may look like
myDict = {'age': ['12'], 'address': ['34 Main Street, 212 First Avenue'],
      'firstName': ['Alan', 'Mary-Ann'], 'lastName': ['Stone', 'Lee']}

def search(values, searchFor):
    for k in values:
        for v in values[k]:
            if searchFor in v:
                return k
    return None

#Checking if string 'Mary' exists in dictionary value
print(search(myDict, 'Lee')); #prints firstName

Now, for the function search(), I do NOT understand exactly how it's working. Could someone please explain it to me???

2 Answers

Hi Colin

see below

#Just an example how the dictionary may look like
myDict = {'age': ['12'], 'address': ['34 Main Street, 212 First Avenue'],
      'firstName': ['Alan', 'Mary-Ann'], 'lastName': ['Stone', 'Lee']}

def search(values, searchFor):
    for k in values: # lopping through every key in the dictionary 
        for v in values[k]: # chekcing the value for each key
            if searchFor in v: # if the value matches your searchFor then return that key
                return k
    return None # not sure what this line is meant to be doing 

#Checking if string 'Mary' exists in dictionary value
print(search(myDict, 'Lee')); #prints firstName


# see my code . if i want to find the address for Mary as an example 

myDict = {'age': ['12'], 'address': ['34 Main Street, 212 First Avenue'], 'firstName': ['Alan', 'Mary-Ann'], 'lastName': ['Stone', 'Lee']}

def search(values, searchFor):
  for key,value in values.items(): # getting each key and val for the dic
    for val in value: # the values are all lists which i can loop around
      if searchFor in val: # if the search term in the list 
        print("{}".format(values['address'])) # print out the value where the key is the address



search(myDict,"Mary")  # returns ['34 Main Street, 212 First Avenue']     

please explain the syntax of the following line: for key,value in values.items(): # getting each key and val for the dic

Why is there a comma (','), between "key" and "value"? What does the "in" statement do?

Sorry but I'm new to python.

Also, just to note, this is a personal project and I haven't actually taken any courses on Python in Treehouse

Hi Colin

dictionary.items() method return a key, value pair which is a tuple. Follow this link for an explanation of tuples http://www.tutorialspoint.com/python/python_tuples.htm.

Say i had a tuple like so (40,50), i could assign these value to variables like so tmp1, tmp2 = (40,50) which assigns the value 40 to tmp1 and 50 to tmp2. In the same way i am saying for key,value in values.items() which return a tuple of the keys and values for that dictionary. The 'in' operator does what says, it looks for something in something.So seeing as the values are all lists i can say, is the search word in the list i.e each val which is a list.

Hope this helps a bit

If your new to python, take the python basics course it will make you life a lot easier.