Overview:
- Redis is an in memory data-structure store, and it supports several data structures and list is one of them.
- The list data structure supported by Redis is implemented as a Linked List. This differs from the Python's implementation of the list.
- In a linked list, operations of reading an element, insertion, deletion in the beginning or at the end takes constant time or in Big O notation O(1), as the head and tail addresses are always available prior to the operation.
- To access an element positioned at Nth location in the list it, takes O(N) time.
- Redis list is a list of strings.
- From a Python program, a Redis list can be manipulated using Redis-Py, which is the Python interface for Redis.
Adding an element to Redis List using the Python API Redis-Py:
- There are several ways a value can be added to a Redis List.
- The LPUSH command adds a value to the head of a Redis list. The time complexity is O(1).
- The RPUSH command adds a value to the tail of a Redis list. The time complexity is O(1).
- The LINSERT command adds a value before or after the first occurence of a specific value. The time complexity is O(N) except for the head element and the tail element.
- The LSET command sets a value for the element specified by the index. The time complexity is O(N) except for the head element and the tail element.
Example 1:
# Sample Python program to demonstrate List operations of Redis and # redis-py import redis
# Create a redis client redisClient = redis.StrictRedis(host='localhost', port=6379, db=0)
# Add values to the Redis list through the HEAD position of the list redisClient.lpush('LanguageList', "Kotlin") redisClient.lpush('LanguageList', "Python")
# Push multiple values through the HEAD of the list redisClient.lpush('LanguageList', "Java", "C++")
# Print the contents of the Redis list while(redisClient.llen('LanguageList')!=0): print(redisClient.lpop('LanguageList')) |
Output:
b'C++' b'Java' b'Python' b'Kotlin' |
Example 2:
# Sample Python program to demonstrate List operations of Redis # and redis-py import redis
# Create a redis client redisClient = redis.StrictRedis(host='localhost', port=6379, db=0)
# Add values to the Redis list through the tail position of the list noSQLList = "NoSQLStores" redisClient.rpush(noSQLList, "Redis") redisClient.rpush(noSQLList, "MongoDB") redisClient.rpush(noSQLList, "Neo4J") redisClient.rpush(noSQLList, "HBase") redisClient.rpush(noSQLList, "Cassandra")
# Add more than one value through the tail of the Redis list redisClient.rpush(noSQLList, "Riak", "CouchDB")
# Print the contents of the Redis list while(redisClient.llen(noSQLList)!=0): print(redisClient.rpop(noSQLList)) |
Output:
b'CouchDB' b'Riak' b'Cassandra' b'HBase' b'Neo4J' b'MongoDB' b'Redis' |
Get the value of an element from Redis List using the Python API Redis-Py:
- LINDEX command returns a value of an element from a Redis list as defined by the index.
- LINDEX does not remove the element from the Redis list.
- The LPOP command removes an element from the head of a Redis list and returns the value of it.
- The RPOP command removes an element from the tail of a Redis list and returns the value of it .
- The LREM command removes an element from a Redis list as specified the index.
Example:
# Sample Python program to demonstrate List operations of Redis # and redis-py import redis
# Create a redis client redisClient = redis.StrictRedis(host='localhost', port=6379, db=0)
# Create a Redis list with all zeros numberList = "numbers" redisClient.lpush(numberList, 0,0,0,0,0)
# Print the Redis list before modification print("Contents of the list before modification:") for i in range(0, redisClient.llen(numberList)): print(redisClient.lindex(numberList, i))
# Modify Redis list contents for i in range(0, redisClient.llen(numberList)): redisClient.lset(numberList, i, i*2)
print("Contents of the list after modification:") # Print the Redis list after modification for i in range(0, redisClient.llen(numberList)): print(redisClient.lindex(numberList, i))
# Empty the Redis list for i in range(0, redisClient.llen(numberList)): redisClient.lpop(numberList)
# Print the length of the list after removing all its elements print("Length of the Redis list after removing all elements:") print(redisClient.llen(numberList)) |
Output:
Contents of the list before modification: b'0' b'0' b'0' b'0' b'0' Contents of the list after modification: b'0' b'2' b'4' b'6' b'8' Length of the Redis list after removing all elements: 0 |
Other list operations on a Redis list using the Python API Redis-Py:
- The LLEN command returns the number of elements present in the Redis list.
- The LTRIM command keeps only a specified range of indexes of a Redis list and removes other elements. Remember 0 is the beginning of the list and -1 is the beginning of the list in reverse order.
Example:
# Sample Python program to demonstrate List operations of Redis # and redis-py import redis
# Create a redis client redisClient = redis.StrictRedis(host='localhost', port=6379, db=0)
# Create a Redis list with few even numbers numberList = "numbers" redisClient.rpush(numberList, 2,4,6,8,10,12)
# Trim the list to have only single digit elements startIndex = 0 endIndex = 3 newList = redisClient.ltrim(numberList, startIndex, endIndex)
# Print the Redis list after trimming print("Single digit even numbers:") for i in range(0, redisClient.llen(numberList)): print(redisClient.lindex(numberList,i))
# Clear the Redis list for i in range(0, redisClient.llen(numberList)): redisClient.lpop(numberList) |
Output:
Single digit even numbers: b'2' b'4' b'6' b'8' |