samedi 16 juillet 2016

Extracting keys and values from a dictionary in Python (in a thread-safe manner)

I have a simple function for extracting keys and values from a dictionary.

def separate_kv_fast(adict):
    '''Separates keys/values from a dictionary to corresponding arrays'''
    return adict.keys(), adict.values() 

I know the order is guaranteed if the dictionary "adict" is not modified between the .keys() and .values() call. What I am wondering is if the return statement guarantees this; basically, is it going to be thread safe?

Is the following construction of "adict" any safer for multi-threading or not-needed?

def separate_kv_fast(adict):
    '''Separates keys/values from a dictionary to corresponding arrays'''
    bdict = dict(adict)
    return bdict.keys(), bdict.values() 

Aucun commentaire:

Enregistrer un commentaire