With both lists and strings, we used the in
and not in
operators to
search for specific values. These expressions returned either True
or
False
depending on if the value was found in the collection.
Example
1 2 3 4 5 6 7 8 9 | greeting = 'Hello, World!'
fruits = ['apple', 'banana', 'pear', 'kiwi', 'orange']
numbers = [28, 32, 7, 29, 33, 0]
print('or' in greeting) # Search for the substring 'or' in the larger string.
print('nana' in fruits) # Search for the value 'nana' in the list of strings.
print('!' not in greeting) # Check if '!' is NOT in 'Hello, World!'.
print(42 not in numbers) # Check if 42 is NOT in the numbers list.
|
Console Output
True
False
False
True
Since the elements in a dictionary come as pairs, we search for an item in either the set of keys or the set of values.
We practiced with the keys()
and values()
methods in the previous two
sections, and we can use them in a similar way to search a dictionary.
Example
1 2 3 4 5 6 7 8 9 10 11 12 | ticket_holders = {
'Bob' : 100,
'Jessi' : 103,
'Maria' : 101,
'Devon' : 102
}
print(101 in ticket_holders.keys())
print(101 in ticket_holders.values())
print('Jessi' in ticket_holders.keys())
print('Jessi' in ticket_holders.values())
|
Console Output
False
True
True
False
101 in ticket_holders.keys()
returns False
. Even
though 101
is in the dictionary, it is a value, and in this case we
are searching through the keys.101 in ticket_holders.values()
returns True
.'Jessi'
first in the
keys and then in the values.If we do not add the keys()
or values()
method after the dictionary
name, Python searches the keys by default.
This means that
search_value in dictionary_name.keys()
returns the same result as
search_value in dictionary_name
Let’s build a quick search algorithm for a phone_book
dictionary. In the
code editor below, begin with the following:
lower()
and capitalize()
methods makes sure that
the string matches the format of the keys in phone_book
. The entries
'john', 'John', and 'JOHN'
all get converted to 'John'
.if/else
block that does the following:name
is one of the keys in phone_book
.True
, print "The number for ___ is ___."
Fill in the first
blank with the entered name (the key). Fill in the second blank with that
person’s phone number (the value).False
, print "Sorry, ___ is not in your phone book."
Fill in
the blank with the entered name.Now expand the program:
On line 20, paste a new input statement:
ph_number = input("Enter a phone number: ")
Add a second if/else
block that:
Checks if ph_number
is NOT a value in the phone_book
.
If True
, prints "___ is not in your phone book!"
. Fill in the
blank with the entered phone number.
If False
, runs the following loop. Be sure to indent it properly
compared to the else
keyword.
26 27 28 | for (key, value) in phone_book.items():
if value == ph_number:
print(f"Dialing {ph_number} will call {key}.")
|
Properly done, your output should look something like this:
Enter a name: kimberly
The number for Kimberly is 555-5509.
Enter a phone number: 333-3333
333-3333 is not in your phone book!
Enter a name: will
Sorry, Will is not in your phone book.
Enter a phone number: 555-5162
Dialing 555-5162 will call Becky.
Update the program to add a new key/value pair to the dictionary if the name
entered by the user is NOT already in phone_book
.