user3227262
user3227262

Reputation: 583

Python redis smemebers is always returning shuffeled set

When I try to get members of a set using python smembers function it is always returning shuffled data. And every call to smembers returns random data. However when I do a smembers through redis cli, it always returns correct sorted data from that set.

print(redis_con.smembers('alpha'))

Upvotes: 0

Views: 594

Answers (2)

for_stack
for_stack

Reputation: 22946

Redis set is unordered, so the result of smembers are NOT sorted. That's a expected behavior. If you want to have sorted result, you need to sort it manually.

Upvotes: 0

Maurice Meyer
Maurice Meyer

Reputation: 18106

redis-py returns a Python set which is per default unordered:

>>> import redis
>>> con = redis.Redis('localhost', 6666)
>>> rSet = con.smembers('xyz')
>>> print(type(rSet))
<class 'set'>

You would need to sort the set yourself:

rSetSorted = sorted(rSet)

Upvotes: 1

Related Questions