1

Hi guys Im new to python coding, and am practicing with lists and dict. I have a code where each name has a set number, and when I enter the name their number gets print out.

name_list = ["bob","jim","james","julie","june"]
number_list = ["1","2","3","4","5"]
name_and_number = dict(zip(name_list, number_list))

def namenumb(something):
    try:
        print("{}'s number is {}".format(
            something, name_and_number[something]))
    except KeyError:
        print("That name doesn't exist"
            .format(namenumb))


while True:
    word = input("> ")
    namenumb(word)

However, I want it to work the other way around aswell, so when I type a number in their name gets printed out. How would I do that?

Thanks

2
  • What do you think? Also this while True: is not a great idea without at least any breaks in the loop. Commented Aug 30, 2014 at 8:59
  • you can use dict.get with a default value to print Commented Aug 30, 2014 at 10:30

3 Answers 3

1

1) You can simply switch your zip statement's arguments and repeat the same

number_and_name = dict(zip(number_list, name_list))
# {'1': 'bob', '3': 'james', '2': 'jim', '5': 'june', '4': 'julie'}

2) Another way of doing is by using index() function

def get_name(number):
    print name_list[number_list.index(number)]
Sign up to request clarification or add additional context in comments.

2 Comments

If I'm not mistaken, .index() runs in linear time, so for large lists it will be noticeably slower than the dictionary approach.
@sredmond, definitely .index() runs in linear time but I figured there are only 5 elements in the list so it shouldn't matter
0
name_list = ["bob","jim","james","julie","june"]
number_list = ["1","2","3","4","5"]
name_and_number = dict(zip(name_list, number_list))

def namenumb(something):
    try:
        for key,value in name_and_number.items():
            if key == something:
                print("{0}'s number is {1}".format(
                                             something, name_and_number[something]))
            elif value == something:
                print("{0}'s name is {1}".format(
                                             something, key))
    except KeyError:
        print("That name doesn't exist"
            .format(namenumb))


while True:
    word = raw_input("> ")
    namenumb(word)

this should be the code what you are looking for I guess.. dict.items() does the work..!!

Comments

0

There is no direct way to do that, but there other ways to do it. First you can iterate over the dict and test if the value is the entered number.

for key, value in name_and_number.items():
    if value == number:
        print("{}'s number is {}".format(key, value))

Second way is to create a other dict with the inversed zip and test it the same way you do.

number_and_name = dict(zip(number_list, name_list))

Another way is to use the index function. With this you can avoid the zip completely. To get the number from the name:

print("{}'s number is {}".format(name, number_list[name_list.index(name)]))

To get the name from the number:

print("{}'s number is {}".format(name_list[number_list.index(number)], number))

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.