skip to Main Content

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'))

2

Answers


  1. 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)
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search