Skip to content

Commit e35821a

Browse files
DOC-4952 added Python scan iterator page
1 parent d806349 commit e35821a

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

content/develop/clients/redis-py/scaniter.md

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ weight: 5
1616
---
1717

1818
Redis has a small family of related commands that retrieve
19-
keys and sometimes their associated values:
19+
keys and, in some cases, their associated values:
2020

2121
- [`SCAN`]({{< relref "/commands/scan" >}}) retrieves keys
2222
from the main Redis keyspace.
@@ -46,21 +46,68 @@ can pass `match`, `count`, and `_type` parameters to `scan_iter()` to constrain
4646
the set of keys it returns (see the [`SCAN`]({{< relref "/commands/scan" >}})
4747
command page for examples).
4848

49-
{{< clients-example scan_iter scan Python >}}
50-
{{< /clients-example >}}
49+
```py
50+
import redis
51+
52+
r = redis.Redis(decode_responses=True)
53+
# REMOVE_START
54+
r.flushall()
55+
# REMOVE_END
56+
57+
r.set("key:1", "a")
58+
r.set("key:2", "b")
59+
r.set("key:3", "c")
60+
r.set("key:4", "d")
61+
r.set("key:5", "e")
62+
63+
for key in r.scan_iter():
64+
print(f"Key: {key}, value: {r.get(key)}")
65+
# >>> Key: key:1, value: a
66+
# >>> Key: key:4, value: d
67+
# >>> Key: key:3, value: c
68+
# >>> Key: key:2, value: b
69+
# >>> Key: key:5, value: e
70+
```
5171

5272
The iterators for the other commands are also named with `_iter()` after
5373
the name of the basic command (`hscan_iter()`, `sscan_iter()`, and `zscan_iter()`).
5474
They work in a similar way to `scan_iter()` except that you must pass a
5575
key to identify the object you want to scan. The example below shows how to
5676
iterate through the items in a sorted set using `zscan_iter()`.
5777

58-
{{< clients-example scan_iter zscan Python >}}
59-
{{< /clients-example>}}
78+
```py
79+
r.zadd("battles", mapping={
80+
"hastings": 1066,
81+
"agincourt": 1415,
82+
"trafalgar": 1805,
83+
"somme": 1916,
84+
})
85+
86+
for item in r.zscan_iter("battles"):
87+
print(f"Key: {item[0]}, value: {int(item[1])}")
88+
# >>> Key: hastings, value: 1066
89+
# >>> Key: agincourt, value: 1415
90+
# >>> Key: trafalgar, value: 1805
91+
# >>> Key: somme, value: 1916
92+
```
6093

6194
Note that in this case, the item returned by the iterator is a
6295
[tuple](https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences)
6396
with two elements for the key and score. By default, `hscan_iter()`
6497
also returns a 2-tuple for the key and value, but you can
6598
pass a value of `True` for the `no_values` parameter to retrieve just
66-
the keys.
99+
the keys:
100+
101+
```py
102+
r.hset("details", mapping={
103+
"name": "Mr Benn",
104+
"address": "52 Festive Road",
105+
"hobbies": "Cosplay"
106+
})
107+
108+
for key in r.hscan_iter("details", no_values=True):
109+
print(f"Key: {key}, value: {r.hget("details", key)}")
110+
# >>> Key: name, value: Mr Benn
111+
# >>> Key: address, value: 52 Festive Road
112+
# >>> Key: hobbies, value: Cosplay
113+
```

0 commit comments

Comments
 (0)