2

How would one iterate the contents of a Shelve?

import Shelve
testShelve = Shelve.open("testShelve")
testShelve["key"] = "value"

for k in testShelve.keys():
    print(k)
jneidel
  • 41
  • 6
  • 2
    The same way you would any other mapping (like a `dict`). – chepner Jul 29 '17 at 16:49
  • Did you read [the documentation](https://docs.python.org/3/library/shelve.html)? “A “shelf” is a persistent, dictionary-like object.” – Dictionary-like means you can use it like a dictionary. – poke Jul 29 '17 at 16:55

1 Answers1

2

As stated above, the Shelve object is dictionary-like and can be used the same. To print the keys and values of the testShelve object:

for key in testShelve:
    print(key, testShelve[key])
Liz Hesser
  • 38
  • 1
  • 5