Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete multiple fields for a key in Redis

Tags:

python

redis

I have the dictionary that is as follows:

dict1 = {"a":"foo","b":"bar","c":"foobar"}

The keys of dict1 are fields of a key in redis. I want to use hdel to delete those fields in Redis. I was wondering if its possible to send multiple fields at once to redis.

I thought of creating a list of keys from dict1 and passing it to redis.hdel but it doesn't work and I am always returned false. Will appretiate any help/suggestions.

list1 = []
key = "somerediskey"
    for k in dict1:
        list1.append(k)
data = redis_connection.hdel(key, list1)
like image 883
hjelpmig Avatar asked Jan 13 '23 08:01

hjelpmig


1 Answers

The Python redis lib accept an hdel(name, *keys), so you should do:

data = redis_connection.hdel(key, *list1)

Also, you don't need to create a list of keys, you can just use dict1.keys():

key = "somerediskey"
data = redis_connection.hdel(key, *dict1.keys())

Related docs: http://kushal.fedorapeople.org/redis-py/html/api.html#redis.StrictRedis.hdel

like image 65
iurisilvio Avatar answered Jan 21 '23 15:01

iurisilvio