@@ -16,7 +16,7 @@ weight: 5
16
16
---
17
17
18
18
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:
20
20
21
21
- [ ` SCAN ` ] ({{< relref "/commands/scan" >}}) retrieves keys
22
22
from the main Redis keyspace.
@@ -46,21 +46,68 @@ can pass `match`, `count`, and `_type` parameters to `scan_iter()` to constrain
46
46
the set of keys it returns (see the [ ` SCAN ` ] ({{< relref "/commands/scan" >}})
47
47
command page for examples).
48
48
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
+ ```
51
71
52
72
The iterators for the other commands are also named with ` _iter() ` after
53
73
the name of the basic command (` hscan_iter() ` , ` sscan_iter() ` , and ` zscan_iter() ` ).
54
74
They work in a similar way to ` scan_iter() ` except that you must pass a
55
75
key to identify the object you want to scan. The example below shows how to
56
76
iterate through the items in a sorted set using ` zscan_iter() ` .
57
77
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
+ ```
60
93
61
94
Note that in this case, the item returned by the iterator is a
62
95
[ tuple] ( https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences )
63
96
with two elements for the key and score. By default, ` hscan_iter() `
64
97
also returns a 2-tuple for the key and value, but you can
65
98
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