Hello coders!! In this article, we will be learning about Python Shelve and the various functions that we can perform in it. In Python, a shelve is a dictionary-like object. It is different from “dbm” databases as the value of shelve can be arbitrary Python objects. So, let us get straight into our topic now.
Different functions available in Python Shelve:
Method | Description |
---|---|
open() | open persistent dictionary object |
close() | synchronize and close persistent dictionary objects. |
sync() | Write back all entries in the cache since the shelf was opened with the writeback set to True. |
get() | returns value associated with a key |
items() | tuples list |
keys() | list of shelf keys |
pop() | remove the specified key and return the corresponding value. |
update() | update shelf from another dictionary/iterable |
values() | list of shelf values |
Let us see certain examples to understand these functions in a better way.
Example 1: Create a database and store values in Python shelve:
import shelve
sh = shelve.open("student")
sh['name'] = "Prachee"
sh['age'] = 21
sh['marks'] = 95
sh.close()
The above code will create a database sample.dir in the current working directory and store the key-value data in the form of the hashtable.
Example 2: Access the value corresponding to the given key:
sh=shelve.open('student')
sh['age']
21
This can also be written as:
sh.get('age')
21
The get() method returns the value which is associated with the particular key.
Example 3: List the items:
list(sh.items())
[ ( ' name ' , ' Prachee ') , ( ' age ' , 21 ) , ( ' marks ' , 95 ) ]
The items() method returns the key with the corresponding values as a list of tuples.
Example 4: List the keys:
list(sh.keys())
[ ' name ' , ' age ' , ' marks ' ]
The keys() method returns the keys in the form of a list.
Example 5: List the values:
list(sh.values())
[ ' Prachee ', 21 , 95 ]
The values() method returns shelf values in the form of a list.
Example 6: Delete an item in Python Shelve:
sh.pop('marks')
list(sh.items())
95 [ ( ' name ' , ' Prachee ' ) , ( ' age ', 21 ) ]
The pop() function removes the item having the given key from the shelf and returns its corresponding value.
Example 7: Update Python Shelve:
up = {'roll':66 , 'dept':'IT'}
sh.update(up)
list(sh.items())
[ ( ' name ' , ' Prachee ' ) , ( ' age ' , 21 ) , ( ' roll ', 66 ) ]
Using the update() method we can update the shelve with some dictionary or iterable.
Python shelve vs pickle:
SHELVE | PICKLE |
---|---|
It is built on top of pickle . It is used to implement a serialization dictionary where the objects are pickled but associated with a key. | It is used to serialize some object as a single-byte stream in a file. |
import shelve vals = [1, 2, 3, 4, 5] with shelve.open('shelfE', 'c') as shelf: shelf['ints'] = vals with shelve.open('shelfE', 'r') as shelf: for key in shelf.keys(): print(repr(key), repr(shelf[key])) | import pickle print(integers) |
OUTPUT: ‘ints’, [1, 2, 3, 4, 5] | OUTPUT: [1, 2, 3, 4, 5] |
Must Read
- 50+ Frequently Asked Python Questions For Interview
- What is Python ord Function
- Working with CRUD in Python
- 【How to】 Check if the Variable Exists in Python
- cPickle in Python Explained With Examples
Conclusion:
With this, we come to an end with this article. I hope that the concept of Python shelves and the various functions that are available for shelf operations were cleared from this article.