Databases 5 min read

Redis Basic Operations with Python: Setting, Getting, Deleting Keys, Hashes, Lists, and Sorted Sets

This tutorial demonstrates how to perform fundamental Redis operations—including setting, retrieving, and deleting keys, working with hashes, lists, and sorted sets—using Python's redis client and includes complete test code for each scenario.

Test Development Learning Exchange
Test Development Learning Exchange
Test Development Learning Exchange
Redis Basic Operations with Python: Setting, Getting, Deleting Keys, Hashes, Lists, and Sorted Sets

1. Set a key‑value pair Scenario: Use Redis to store a simple key and value. import redis def test_set_key_value(): r = redis.Redis(host='localhost', port=6379, db=0) r.set('key', 'value') assert r.get('key') == b'value'

2. Get a key’s value Scenario: Retrieve the value associated with a key. import redis def test_get_key_value(): r = redis.Redis(host='localhost', port=6379, db=0) value = r.get('key') assert value == b'value'

3. Delete a key Scenario: Remove a key from Redis. import redis def test_delete_key(): r = redis.Redis(host='localhost', port=6379, db=0) r.delete('key') assert r.exists('key') is False

4. Check key existence Scenario: Verify whether a key exists. import redis def test_check_key_existence(): r = redis.Redis(host='localhost', port=6379, db=0) exists = r.exists('key') assert exists is True

5. Set a hash table Scenario: Store multiple field‑value pairs in a Redis hash. import redis def test_set_hash(): r = redis.Redis(host='localhost', port=6379, db=0) r.hset('hash', mapping={'field1': 'value1', 'field2': 'value2'}) assert r.hget('hash', 'field1') == b'value1'

6. Get a value from a hash Scenario: Retrieve a specific field from a hash. import redis def test_get_hash_value(): r = redis.Redis(host='localhost', port=6379, db=0) value = r.hget('hash', 'field1') assert value == b'value1'

7. List all hash fields Scenario: Obtain all field names of a hash. import redis def test_list_hash_fields(): r = redis.Redis(host='localhost', port=6379, db=0) fields = r.hkeys('hash') assert b'field1' in fields and b'field2' in fields

8. Add elements to a list Scenario: Push multiple items onto a Redis list. import redis def test_add_list_element(): r = redis.Redis(host='localhost', port=6379, db=0) r.rpush('list', 'element1', 'element2') assert r.llen('list') == 2

9. Retrieve an element from a list Scenario: Pop the first element from a list. import redis def test_get_list_element(): r = redis.Redis(host='localhost', port=6379, db=0) element = r.lpop('list') assert element == b'element1'

10. Set a sorted set Scenario: Create a sorted set with scores. import redis def test_set_sorted_set(): r = redis.Redis(host='localhost', port=6379, db=0) r.zadd('sorted_set', {'item1': 1, 'item2': 2}) assert r.zscore('sorted_set', 'item1') == 1

All examples assume a locally running Redis instance on the default port 6379; adjust connection parameters as needed.

PythonDatabaseTestingRedisKey-Value Store
Test Development Learning Exchange
Written by

Test Development Learning Exchange

Test Development Learning Exchange

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.