简体   繁体   中英

How to get Values by Keys in Dictionary in For loop Python

I have got this code:

mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')
print(mylist[char])

and it works fine, but when I change it to this:

mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')
for char in mylist:
    if mylist[char] == char:
        print(mylist[char])

it dos not return any value, neither gives error message.

What is missing or wrong?

Thank you!

BR, Valters

First, don't call 2 things the same here char . As you iterate the dict you get the keys and it's with these keys that you need to check for equality

mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')
for key in mylist:
    if key == char:
        print(mylist[key])

But doing that make the all purpose of using a dict disappear, you loose the performance of just doing mylist[char]

mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')
if char in mylist.keys():
  print mylist.get(char, None)

In your code you are actually trying to compare your key with dictionary value, and there it fails.

I hope I answered your problem.

The char in the loop scoop is referring to a key of the mylist so when you are doing in the loop if mylist[char] == char it's checking if the key is equal to the value.
This is how it looks like inside the loop on each iteration.

mylist[char] 1 char a
mylist[char] 2 char b
mylist[char] 3 char c

so in the second example, it's not getting into the if statement that will create you a list of all lines that you can iterate over it over and over again.
You can do

mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')
if char in mylist:
    print(mylist[char])
mylist = {'a':1,'b':2,'c':3}
char = input('Enter char: ')

for char in mylist:
   if mylist[char] == char:
      print(mylist[char])

The problem with this is, when you are looping through a dictionary, you are checking if key==value which returns false and so nothing gets printed.

for char in mylist Here the char value is the keys so for your approach you have to modify the if condition. Instead of checking keys with value check keys with the entered chars.

So this will be like:

for key in mylist:
   if key == char:
      print(mylist[char])

And also instead of using loop and if condition, you can simply do as below:

if char in mylist:  # it checks if char is present in dict's key or not
   print(mylist[char])

yes it wont work the reason is

mylist = {'a':1,'b':2,'c':3}

char = input('Enter char: ') // <- here you are assigning the user input to the variable char.

for char in mylist: // <- the variable char is again assigned different value ie the keys from the dictionary

if mylist[char] == char: // <- so here in first iteration the char will be "a" in " mylist[char] ", which is compared with char "a", so the condition is as follows.

mylist["a"] == a // <- but here the value will be " 1 == a " since this condition fails

print(mylist[char]) // <- the print statement is not executed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM