redissortedset

How can I fix the 'AttributeError: 'int' object has no attribute 'items'' in Redis when using zadd() to add values to a sorted set?


import redis r= redis.StrictRedis(host="localhost", port=6379, db=0, charset="utf-8", decode_responses=True) r.zadd("players", 10, "new_player")

""" Traceback (most recent call last): File "...\sorted_set.py", line 6, in x= r.zadd("players", 10, "new_player") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "...\AppData\Local\Programs\Python\Python311\Lib\site-packages\redis\commands\core.py", line 4109, in zadd for pair in mapping.items(): ^^^^^^^^^^^^^ AttributeError: 'int' object has no attribute 'items' """


Solution

  • If you check the documentation of redis-py, the members of the sorted set have to be provided as a dictionary. Full documentation here

    The sample code would look like:

    import redis
    r = redis.StrictRedis(host="localhost", port=6379,db=0)
    x = r.zadd("players", {"new_player":10})