-1

i am trying to create a script with a loop and a conditional statement to find the value of the number of dogs in the following collection of key-value pairs but i am just not getting my head around it.

so how do i find the value of the number of dogs in the following collection of key-value pairs,

Cheers Vlad


 for value in KC.values():
  if 'Dogs' in value:
       return value
boomkings
  • 1
  • 1
  • `Dog` isn't a value, its a (capitalized version of) key, whats wrong with `KC.get('dogs')`? – Sayse Nov 16 '21 at 10:44
  • did you print values? You'll get something like 7,12,21,3,3 ... no "Dogs" in it. – Patrick Artner Nov 16 '21 at 10:45
  • 1
    You do not need to loop - simply get the value: `print( KC.get("Dogs","Not inside dictionary"))` - you also have a spelling mistake in your key, they need to match EXACTLY - with a normal `KC["Dogs"]` you get an exception, dict.get(...) is more lenient and allows to get a default value instead – Patrick Artner Nov 16 '21 at 10:46
  • @Sayse i am looking for an if statement and a loop to be included in this, do you have any suggestions how to write it like that? – boomkings Nov 16 '21 at 10:47
  • If you want to make your serach case-insentive you could use `value.lower()` and 'Dogs.lower()` (or just type your search string in lowercase). – white Nov 16 '21 at 10:48
  • @PatrickArtner thanks Patric, but i am looking for it to be a Loop and an if statement for a task in college, any suggestions? – boomkings Nov 16 '21 at 10:49
  • You can also get tuples of (key, value) from your dict: `print(KC.items())` - you could construct something loop-ish with that and use comparisons on lower-cased strings of the first item in the tuple (the key) - but then you forgoe any advantages of the dict to begin with. – Patrick Artner Nov 16 '21 at 10:49
  • Any ideas for your task? Yeah: do it, don't ask here - you got enough pointers and methods of dicts to solve it. Also: SyntaxError - you use return but are not inside a function - so your [mre] is seriously faulty. – Patrick Artner Nov 16 '21 at 10:50
  • for loops and conditionals to make sense you would need to use some other data structure (like nested (2d) lists) not a dictionary – Matiiss Nov 16 '21 at 10:52
  • Possible Duplicate: [case-insensitive-dictionary-search](https://stackoverflow.com/questions/3296499/case-insensitive-dictionary-search) – Patrick Artner Nov 16 '21 at 10:54

1 Answers1

1

You have .values(), which will give you the values. E.g 7, 12, 21, 3, 3

Otherwise you can use .keys(), which will give you "Cats", "hamsters" etc.

Or you can simply check:

if "dogs" in KC:
    return KC["dogs"]

Keep in mind that coding is case sensitive. So "Dogs" and "dogs" are not the same.

ziarra
  • 137
  • 6
  • 4
    the same thing can be achieved with a simple `return KC.get('dogs')` without any if statements – Matiiss Nov 16 '21 at 10:49
  • KC= {'Cats':7,'hamsters':12,'Birds':21,'dogs':3,'Chicken':3 } for value in KC.values(): if "dogs" in KC: return KC["dogs"] would this be a correct one? – boomkings Nov 16 '21 at 10:52
  • 1
    No boomking - it would needlessly iterate all values if the key is not in the dict and return on the first iteration if it is in the dict which makes no sense at all – Patrick Artner Nov 16 '21 at 10:52
  • so how do i find the value of the number of dogs in the following collection of key-value pairs, apologies i have been at it for too long, my head is just spinning :( – boomkings Nov 16 '21 at 10:56
  • @boomkings you want the number to be the key? also use `KC.get('dogs')` to get the number of dogs and don't use loops or conditionals. Unless you need case insensitivity but that would be done slightly differently anyways – Matiiss Nov 16 '21 at 10:57
  • and if i used a loop and an if how would i do that @Matiiss – boomkings Nov 16 '21 at 10:58
  • @boomkings you can do sth like `for key, value in KC.items(): if key == 'dogs': return value` but that is completely needless if using a dictionary (unless case insensitivity matters or sth else), such a loop better fits some 2d list – Matiiss Nov 16 '21 at 11:00