|
| 1 | +"""Working with list values.""" |
| 2 | +from redis_python_tutorial.logging import logger |
| 3 | + |
| 4 | + |
| 5 | +def list_values_demo(r): |
| 6 | + """Push and pop items from a list.""" |
| 7 | + # Add single string to a new list. |
| 8 | + r.lpush('my_list', 'A') |
| 9 | + logger.info(f"my_list: {r.lrange('my_list', 0, -1)}") |
| 10 | + # Push second string to list from the right. |
| 11 | + r.rpush('my_list', 'B') |
| 12 | + logger.info(f"my_list: {r.lrange('my_list', 0, -1)}") |
| 13 | + # Push third string to list from the right. |
| 14 | + r.rpush('my_list', 'C') |
| 15 | + logger.info(f"my_list: {r.lrange('my_list', 0, -1)}") |
| 16 | + # Remove 1 instance from the list where the value equals 'C'. |
| 17 | + r.lrem('my_list', 1, 'C') |
| 18 | + logger.info(f"my_list: {r.lrange('my_list', 0, -1)}") |
| 19 | + # Push a string to our list from the left. |
| 20 | + r.lpush('my_list', 'C') |
| 21 | + logger.info(f"my_list: {r.lrange('my_list', 0, -1)}") |
| 22 | + # Pop first element of our list and move it to the back. |
| 23 | + r.rpush('my_list', r.lpop('my_list')) |
| 24 | + logger.info(f"my_list: {r.lrange('my_list', 0, -1)}") |
| 25 | + return r.lrange('my_list', 0, -1) |
0 commit comments