-1

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'}]
Michal K
  • 69
  • 1
  • 7

3 Answers3

1

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)
thisisayush
  • 292
  • 1
  • 8
1

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]
azro
  • 53,056
  • 7
  • 34
  • 70
  • list comprehension is what I see most helpful here and most manageable. Thanks – Michal K Jan 01 '22 at 09:58
  • azro - if you could just a little bit of explanation here or some external source to find out about I don't know how can I call it - "nested?" dict comprehension in list comprehension? – Michal K Jan 01 '22 at 10:05
  • Note that `keep` does not need to be a `set` as you are **looping** through it, not testing membership, it would work as fast with a `tuple`. – mozway Jan 01 '22 at 10:11
  • 1
    @MichalK it's quite the same as one level, maybe see https://stackoverflow.com/questions/18072759/list-comprehension-on-a-nested-list – azro Jan 01 '22 at 10:19
0

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)
MK14
  • 481
  • 3
  • 9