Got some basic problems with looping through the list of dictionaries. I want to loop through the list called jslist and get an output as I show at the bottom. Basically I want to extract chosen key: value pairs (the ones I paste as an example below - site_id, sitekey, nickname) from the list and store them in another list of dicts.
jslist = [
{'site_id': '1111111', 'hostname': 'abc.com', 'nickname': 'abc.com', 'sitekey': '29346385345', 'sitekey_admin': '293857349857934857345', 'timezone': '1', 'visitors_online': '1', 'visitors_today': '34'},
{'site_id': '100992048', 'hostname': 'gcd.com', 'nickname': 'gcd.com', 'sitekey': '938573945739453', 'sitekey_admin': '20395734985793', 'timezone': '1', 'visitors_online': '0', 'visitors_today': '2'}]
dict_1 = {}
for k in jslist:
dict_1['site_id'] = k['site_id']
dict_1['sitekey'] = k['sitekey']
dict_1['nickname'] = k['nickname']
print(list(dict_1))
current output:
{'site_id': '100992048', 'sitekey': '938573945739453', 'nickname': 'gcd.com'}
expected output
[{'site_id': '100992048', 'sitekey': '938573945739453', 'nickname': 'gcd.com'},{'site_id': '1111111', 'sitekey': '29346385345', 'nickname': 'abc.com'}]
CodePudding user response:
You're using one dict for each, so you keep appending the same instance, the dict creation should be inside the loop. Also you can store the needed keys in a list to made the code easier to update and proper
Combining with a list comprehension
keep = {'site_id', 'sitekey', 'nickname'}
jslist_new = [{k: item[k] for k in keep} for item in jslist]
CodePudding user response:
You're just updating the same dict instead of pushing the dict to new list. Here's how:
jslist = [
{'site_id': '1111111', 'hostname': 'abc.com', 'nickname': 'abc.com', 'sitekey': '29346385345', 'sitekey_admin': '293857349857934857345', 'timezone': '1', 'visitors_online': '1', 'visitors_today': '34'},
{'site_id': '100992048', 'hostname': 'gcd.com', 'nickname': 'gcd.com', 'sitekey': '938573945739453', 'sitekey_admin': '20395734985793', 'timezone': '1', 'visitors_online': '0', 'visitors_today': '2'}]
jslist_new = []
for k in jslist:
dict_1 = {}
dict_1['site_id'] = k['site_id']
dict_1['sitekey'] = k['sitekey']
dict_1['nickname'] = k['nickname']
jslist_new.append(dict_1)
print(jslist_new)
CodePudding user response:
You can store the required values in a temporary dictionary and append it to a list... like this
jslist = [
{'site_id': '1111111', 'hostname': 'abc.com', 'nickname': 'abc.com', 'sitekey': '29346385345', 'sitekey_admin': '293857349857934857345', 'timezone': '1', 'visitors_online': '1', 'visitors_today': '34'},
{'site_id': '100992048', 'hostname': 'gcd.com', 'nickname': 'gcd.com', 'sitekey': '938573945739453', 'sitekey_admin': '20395734985793', 'timezone': '1', 'visitors_online': '0', 'visitors_today': '2'}]
res = []
for dct in jslist:
temp = dict()
for key, val in dct.items():
if key in ("site_id", "sitekey", "nickname"):
temp[key] = val
res.append(temp)
print(res)
You can compress it to a list comprehension like this...
jslist = [
{'site_id': '1111111', 'hostname': 'abc.com', 'nickname': 'abc.com', 'sitekey': '29346385345', 'sitekey_admin': '293857349857934857345', 'timezone': '1', 'visitors_online': '1', 'visitors_today': '34'},
{'site_id': '100992048', 'hostname': 'gcd.com', 'nickname': 'gcd.com', 'sitekey': '938573945739453', 'sitekey_admin': '20395734985793', 'timezone': '1', 'visitors_online': '0', 'visitors_today': '2'}]
res = [{key:val for key, val in dct.items() if key in ("site_id", "sitekey", "nickname")} for dct in jslist]
print(res)