Apply function to specific element's value of a list of dictionaries

Question:

tbl_headers = db_admin.execute("SELECT name, type FROM PRAGMA_TABLE_INFO(?);", table_name)

tbl_headers is same below:

[{'name': 'id', 'type': 'INTEGER'}, {'name': 'abasdfasd', 'type': 'TEXT'}, {'name': 'sx', 'type': 'TEXT'}, {'name': 'password', 'type': 'NULL'}, {'name': 'asdf', 'type': 'TEXT'}]

I need apply hash_in() function on the 'name' values are in dictionary elements of above list.

Have tried these:

tbl_headers = [hash_in(i['name']) for i in tbl_headers]

suppresses dictionaries and return only a list of ‘name’ values:

['sxtw001c001h', 'sxtw001c001r001Z001e001c001r001Z001a001Z', 'sxtw001w001r', 'sxtw001c001q001n001v001r001r001Z001o', 'sxtw001e001c001r001Z']

OR

tbl_headers = map(hash_in, tbl_headers)

Returns error.

Update

The Output result I have seek is same:

[{'name': hash_in('id'), 'type': 'INTEGER'}, {'name': hash_in('abasdfasd'), 'type': 'TEXT'}, {'name': hash_in('sx'), 'type': 'TEXT'}, {'name': ('password'), 'type': 'NULL'}, {'name': ('asdf'), 'type': 'TEXT'}]

Appreciate you.

Asked By: parmer_110

||

Answers:

Try this list comprehension:

tbl_headers = [{'name': hash_in(i['name']), 'type': i['type']} for i in tbl_headers]
Answered By: Jamiu Shaibu
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.