Skip to content

Commit c76e12f

Browse files
committed
Create listExamples.py
1 parent b060cdc commit c76e12f

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

listExamples.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import redis
2+
3+
# init the redis obj
4+
r = redis.Redis(host="127.0.0.1",
5+
port=6379,
6+
db=0,
7+
decode_responses=True)
8+
9+
10+
# add some elements into the list
11+
r.lpush("books", *("clean code", "Code Complete", "Peopleware"))
12+
13+
14+
# get item at index
15+
print(r.lindex("books", 2))
16+
17+
18+
# Get the length of the list
19+
respllen = r.llen(name="books")
20+
print("length of list books is ", r.llen("books"))
21+
22+
23+
# Get a subset of the list items including start & end
24+
# index starts at 0
25+
# even if index is out of bound, returns till last element.
26+
# Can also use negitive numbers, same like python slicing
27+
resplrange = r.lrange(name="books", start=0, end=-2)
28+
print(resplrange)
29+
30+
31+
# LPOP returns first element while RPOP returns last element
32+
print(r.lpop(name='books'))
33+
print(r.rpop(name='books'))
34+
35+
36+
r.flushdb()

0 commit comments

Comments
 (0)