I have a list
of dictionaries. From each of the dictionaries, I want to extract information of some of the keys which I saved in a list beforehand.
I can do it with a for
-loop, but my list
length is 15,504,603. It requires a very long time to process. I am looking for alternative ways of doing it.
My list of dictionaries (in reality this is query_set.QuerySet
):
data = [
{'name': 'Alex', 'employee_id': 1110, 'age': 38, 'rank': 'CEO', 'salary': 'unknown'},
{'name': 'Monty', 'employee_id': 1111, 'age': 33, 'rank': 'EO', 'salary': 2400},
{'name': 'John', 'employee_id': 1114, 'age': 32, 'rank': 'EO', 'salary': 2200},
{'name': 'Max', 'employee_id': 1120, 'age': 26, 'rank': 'OA', 'salary': 1200},
{'name': 'Ginee', 'employee_id': 1130, 'age': 28, 'rank': 'OA', 'salary': 1200},
{'name': 'Adam', 'employee_id': None, 'age': 18, 'rank': 'summer_intern', 'salary': None}
]
The information I want to extract are 'name'
, 'age'
and 'rank'
So I make a list of keys beforehand:
info = ['name', 'age', 'rank']
I could do the task by performing a for loop
result = []
result.append(info)
for i in range(len(data)):
output = [data[i][x] for x in info]
result.append(output)
and finally
for item in result:
print("\t".join(map(str,(item))))
and the result goes like:
name age rank
Alex 38 CEO
Monty 33 EO
John 32 EO
Max 26 OA
Ginee 28 OA
Adam 18 summer_intern
In reality there are 15504603 dictionaries with 43 key : value
within my list which is taking very long time to process. i.e. 22661/15504603 after ~2 hour of running.
What could be the ideal and time saving ways of doing this?