0

I have a few dictionaries that I added into one larger dictionary.

violet = {"animal":"cat","owner":"Theresa"}
lucy = {"animal":"cat","owner":"Theresa"}
button = {"animal":"rabbit","owner":"Theresa"}
our_animals = {}
our_animals["Violet"] = violet
our_animals["Lucy"] = lucy
our_animals["Button"] = button
for animal in our_animals:
        print(animal,"is a:")
        for kind, owner in our_animals.values():
            print(kind,"owned by", owner)

This just gave me this

Violet is a:
animal owned by owner
animal owned by owner
animal owned by owner
Lucy is a:
animal owned by owner
animal owned by owner
animal owned by owner
Button is a:
animal owned by owner
animal owned by owner
animal owned by owner

In my mind I've now created a dictionary in which

"Violet" is a key and the dictionary {"animal":"cat,"owner":"Theresa"} is the value associated with "Violet". In the code above I was attempting to iterate over the values of the dictionary which is itself a value. I wanted my output to be something like.

Violet is a:
cat owned by Theresa
Lucy is a:
cat owned by Theresa
Button is a:
rabbit owned by Theresa

I'm only a month into learning python so any teaching moment would be greatly appreciated. Thank you in advance!

Elodin86
  • 35
  • 4

4 Answers4

2

You can change your first loop to iterate over key,value pairs and then access the owner and animal type via value['animal'] & value['owner'].

for animal_name, animal_data in our_animals.items():
    print(animal_name, 'is a:')
    print(animal_data['animal'], 'owned by', animal_data['owner'])

Using dict.items() to iterate over your key and value pairs here would be the best approach.

Looping Dictionaries

Output:

Violet is a:
cat owned by Theresa
Lucy is a:
cat owned by Theresa
Button is a:
rabbit owned by Theresa
PacketLoss
  • 5,561
  • 1
  • 9
  • 27
  • Thank you for the help! If I understand correctly, I use `dic.items()` to iterate over both key and value pairs, and then under the value pair section I'm telling python to print the value paired with key `['animal']` and key `['owner']`? – Elodin86 Oct 06 '20 at 19:08
  • Correct. When you iterate over `d.items()` it provides a key value pair back, then since your value is a `dict` you can just access it via value['owner']. I left a link in the question so you can understand this a bit more indepth. – PacketLoss Oct 06 '20 at 22:31
1

Try this,

violet = {"animal":"cat","owner":"Theresa"}
lucy = {"animal":"cat","owner":"Theresa"}
button = {"animal":"rabbit","owner":"Theresa"}
our_animals = {}
our_animals["Violet"] = violet
our_animals["Lucy"] = lucy
our_animals["Button"] = button
for animal in our_animals:
   print(animal,"is a:")
   print(our_animals[animal]['animal'],"owned by", our_animals[animal]['owner'])
Maran Sowthri
  • 829
  • 6
  • 14
  • 1
    It's good to note here that this way of looping will only iterate over `keys` which makes accessing values a bit less readable. If you need to access `keys` and `values` just iterate over `d.items()` instead. [Looping Dictionaries](https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops) – PacketLoss Oct 06 '20 at 05:06
0

You can use something like this -

for key, values in zip(our_animals.keys(), our_animals.values()):
        print(key,"is a:")
        print(values["animal"],"owned by", values["owner"])
Afif Al Mamun
  • 199
  • 1
  • 11
  • 2
    You shouldn't use `zip` here when you can simply call the built-in `dict.items()` – PacketLoss Oct 06 '20 at 04:58
  • Thank you for your comment. I just added it here as I thought it would be more readable to the OP. – Afif Al Mamun Oct 06 '20 at 05:02
  • I would argue that it less readable, especially to someone new to python who may have no idea what `zip` is. Would be best to point the OP in the correction direction with the reasons why, as they are still learning and going forward it would benefit them to understand the best way to iterate over a valuable data structure. – PacketLoss Oct 06 '20 at 05:12
  • This code also won't work prior to 3.7. (or maybe 3.6). There is no guarantee that the values returned by .keys() are returned in the same order as those returned by .values(). Use .items(). It's guaranteed always. – Frank Yellin Oct 06 '20 at 06:17
0

Do something like this

for key,value in our_animals.items():

  print(str(key)+' is a : ');

  print(str(value['animal'])+' owned by '+str(value['owner']));