-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlist.py
36 lines (27 loc) · 1.1 KB
/
list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Working with list values."""
from redis import Redis
from redis_python_tutorial.logger import LOGGER
def list_values_demo(r: Redis):
"""
Create and modify a Redis list.
:param Redis r: Remote Redis instance.
"""
"""Push and pop items from a list."""
# Add single string to a new list.
r.lpush("my_list", "A")
LOGGER.info(f"my_list: {r.lrange('my_list', 0, -1)}")
# Push second string to list from the right.
r.rpush("my_list", "B")
LOGGER.info(f"my_list: {r.lrange('my_list', 0, -1)}")
# Push third string to list from the right.
r.rpush("my_list", "C")
LOGGER.info(f"my_list: {r.lrange('my_list', 0, -1)}")
# Remove 1 instance from the list where the value equals 'C'.
r.lrem("my_list", 1, "C")
LOGGER.info(f"my_list: {r.lrange('my_list', 0, -1)}")
# Push a string to our list from the left.
r.lpush("my_list", "C")
LOGGER.info(f"my_list: {r.lrange('my_list', 0, -1)}")
# Pop first element of our list and move it to the back.
r.rpush("my_list", r.lpop("my_list"))
LOGGER.info(f"my_list: {r.lrange('my_list', 0, -1)}")