Adding strings from json object to state together efficiently?

So, I have an array of json objects that looks like this:

data = [{key1: 123, key2:"this is the first string to concatenate"},
 {key1: 131, key2:"this is the second string to concatenate"},
 {key1: 152, key2:"this is the third string to concatenate"} ] 

      

basically, I'm using a for loop right now like this:

all_key2 = ""
data = json.load(json_file)
for p in data: 
    #make it all one big string 
    if langid.classify(p["key2"])=="english": 
        all_key2 = p["key2"] + " " + all_key2 

      

so the answer should be:

"this is the first string to concatenate this is the second string to concatenate this is the third string to concatenate" 

      

But it takes a long time because I have bajillion objects and long strings. Is there a faster way to accomplish this concatenation?

[EDIT] Studied lambda functions , maybe this is the way to go?

+3


source to share


1 answer


all_key2 = " ".join([elem["key2"] for elem in data if langid.classify(elem["key2"])=="english"])

      



+4


source







All Articles