Redis Set- Add, Remove elements using Python and Redis-Py

Overview:

  • Redis is an in-memory data structure.
  • Redis supports several data structures. The set is one among them.
  • It represents the mathematical concept of a set.
  • The set supported by Redis is a set of strings.
  • The Redis set supports several set operations like intersection, set subtraction, union of two sets.
  • From a Python program redis-py module can be used to interface with a Redis server.

 

Adding elements to a Redis set:

  • The Redis command SADD adds an element to a set.
  • The Redis command SCARD returns the cardinality of the set. The cardinality of a set is the number of elements present in it.
  • The Redis command SMEMBERS returns all the elements present in a set.
  • This Python example uses the equivalent methods provided by redis-py for the Redis commands SADD, SCARD and SMEMBERS.

 

Example:

# Sample Python program to demonstrate the set data structure supported by Redis

 

# import the python interface for Redis: The redis-py

import redis

 

# Create a redis client

redisClient = redis.StrictRedis(host='localhost',

                                port=6379,

                                db=0)

# The name of the Redis set

colorSet = "Colors"

 

# Add elements to the Redis set

redisClient.sadd(colorSet, "Red")

redisClient.sadd(colorSet, "Orange")

redisClient.sadd(colorSet, "Yellow")

redisClient.sadd(colorSet, "Green")

redisClient.sadd(colorSet, "Blue")

redisClient.sadd(colorSet, "Indigo")

redisClient.sadd(colorSet, "violet")

 

# Print the cardinality of the Redis set

print("Cardinality of the Redis set:")

print(redisClient.scard(colorSet))

 

# Print all members of the Redis set

print("Contents of the Redis set:")

print(redisClient.smembers(colorSet))

 

Output:

Cardinality of the Redis set:

7

Contents of the Redis set:

{b'Indigo', b'violet', b'Orange', b'Yellow', b'Red', b'Green', b'Blue'}

MyMac:dir1 usr1$ python3 set1.py

Cardinality of the Redis set:

7

Contents of the Redis set:

{b'Yellow', b'Green', b'Red', b'Blue', b'violet', b'Orange', b'Indigo'}

 

Removing elements from a Redis set:

  • The command SPOP removes an element from a Redis list and returns the value of the removed element.
  • The SREM command removes an element with a specified value from the set.

Example:

# Sample Python program to remove elements from a Redis set, using redis-py

import redis

 

# Create a redis client

redisClient = redis.StrictRedis(host='localhost',

                                port=6379,

                                db=0)

# The name of the Redis set

colors = "Hues"

 

# Add elements to the Redis set

redisClient.sadd(colors, "Red")

redisClient.sadd(colors, "Green")

redisClient.sadd(colors, "Blue")

redisClient.sadd(colors, "Yellow")

 

# Print the members of the Redis set before removing an element

print("Initial contents of the Redis set:")

print(redisClient.smembers(colors))

 

# Remove an element from a Redis set

redisClient.srem(colors, "Yellow")

 

# Print the members of the Redis set after removing an element

print("Contents of the Redis set after removing an element:")

print(redisClient.smembers(colors))

 

# Remove the elements one by one

while(redisClient.scard(colors) > 0):

    print("Removing {}...".format(redisClient.spop(colors)))

 

print("Length of the Redis set after removing all the elements:")

print(redisClient.scard(colors))

Output:

Initial contents of the Redis set:

{b'Blue', b'Yellow', b'Green', b'Red'}

Contents of the Redis set after removing an element:

{b'Blue', b'Green', b'Red'}

Removing b'Red'...

Removing b'Blue'...

Removing b'Green'...

Length of the Redis set after removing all the elements:

0


Copyright 2024 © pythontic.com