Python Forum
Iterate through a list of dictionary and append a new value.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterate through a list of dictionary and append a new value.
#1
I'm trying to iterate through the following dictionary and append in the list .

dic={'Dining': [{'Switchboard': [{'Fan': ['01:45.9166666666']},
                             {'Covelight': ['01:116.5']},
                             {'Light': ['01:182.4']}]}],
 'Kids': [{'Switchboard': [{'Light': ['01:206.8']},
                           {'Fan': ['01:2.58333333333']}]}],
 'Kitchen': [{'MultiSensor': [{'Red': ['01:0.95125']}]},
             {'Switchboard': [{'Light2': ['01:125.2']},
                              {'Light': ['01:176.533333333']},
                              {'Fan': ['01:5.0']}]}],
 'Living': [{'Switchboard': [{'Light': ['01:7.13333333333']},
                             {'Covelight': ['01:17.6']},
                             {'Fan': ['01:6.91666666667']}]},
            {'MultiSensor': [{'Red': ['01:0.508333333333']},
                             {'Green': ['01:4.23333333333']}]}]}
for example if I want to append '02:3.9' in {'Green': ['01:4.23333333333']}
Reply
#2
I would say that structure of data is rather complicated.

There are several layers of lists and following code relies on order (will break when order is changed or elements are added to lists; accessing last element in lists):

>>> dic['Living'][-1]['MultiSensor'][-1]['Green'].append('02:3.9')
By changing the code a little bit one can rely only on order and allow appending (utilizing indexes from start; accessing second element in lists)

>>> dic['Living'][1]['MultiSensor'][1]['Green'].append('02:3.9')
List of dictionaries is most useful when dictionaries are of similar structure.

With slightly adjusting datastructure (eliminating lists of dictionaries) it can be made more robust against possible additions, changes:

dic={'Dining': {'Switchboard': {'Fan': ['01:45.9166666666'], 'Covelight': ['01:116.5'], 'Light': ['01:182.4']}},
     
     'Kids': {'Switchboard':  {'Light': ['01:206.8'], 'Fan': ['01:2.58333333333']}},
     'Kitchen': {'MultiSensor': {'Red': ['01:0.95125']},
                 'Switchboard': {'Light2': ['01:125.2'], 'Light': ['01:176.533333333'], 'Fan': ['01:5.0']}},
     'Living': {'Switchboard': {'Light': ['01:7.13333333333'], 'Covelight': ['01:17.6'], 'Fan': ['01:6.91666666667']},
                'MultiSensor': {'Red': ['01:0.508333333333'], 'Green': ['01:4.23333333333']}}}



>>> dic['Living']['MultiSensor']['Green']   # you can append to that
['01:4.23333333333']
>>> dic['Kitchen']['Switchboard']['Fan']
['01:5.0']
>>> dic['Kitchen']['MultiSensor']['Green'] = ['01:5.123'] # add new key-value pair under 'MultiSensor'
>>> dic['Kitchen']['MultiSensor']
{'Red': ['01:0.95125'], 'Green': ['01:5.123']}
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  append str to list in dataclass flash77 6 463 Mar-14-2024, 06:26 PM
Last Post: flash77
  Dictionary in a list bashage 2 540 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 666 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Sort a list of dictionaries by the only dictionary key Calab 1 488 Oct-27-2023, 03:03 PM
Last Post: buran
  How to add list to dictionary? Kull_Khan 3 998 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
Question How to append integers from file to list? Milan 8 1,444 Mar-11-2023, 10:59 PM
Last Post: DeaD_EyE
  iterate through the dict_values while unpacking the dictionary PeacockOpenminded 3 1,291 Jan-22-2023, 12:44 PM
Last Post: PeacockOpenminded
  read a text file, find all integers, append to list oldtrafford 12 3,515 Aug-11-2022, 08:23 AM
Last Post: Pedroski55
  Using .append() with list vs dataframe Mark17 7 10,344 Jun-12-2022, 06:54 PM
Last Post: Mark17
  check if element is in a list in a dictionary value ambrozote 4 1,962 May-11-2022, 06:05 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020