Python Forum
Iterating list of oredereddict for creating new list of ordereddict
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterating list of oredereddict for creating new list of ordereddict
#2
List of dictionaries makes usually sense when keys are the same. Then it's easy to manipulate data with comprehension.

This structure is prone to errors (what if new dictionaries will be added? new dictionaries with some keys overlapping with existing ones etc). I recommend to think about your needs and determine whether there can be better ways to store data.

It can be done manually, but this is not generalised solution, it works on this particular dataset and may break if changes are made (code assumes that any match will suffice, not all must match):

>>> from collections import OrderedDict
>>> lst = [OrderedDict([('Numbers', '15'), ('FirstName', 'John'), ('SecondName', 'Raul'), ('MiddleName', 'Kyle')]),
...        OrderedDict([('Names', 'John'), ('NewFirstName', 'Mark'), ('NewSecondName', 'Sachel'), ('NewThirdName', 'Raul')])]
>>> value_to_search = lst[0]['SecondName']
>>> if value_to_search in lst[1].values():
...     print(lst[0]['Numbers'])
...
15
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


Messages In This Thread
RE: Searching for keys and values in ordereddict - by perfringo - May-02-2019, 05:19 AM
Edit ordereddict - by babypython - May-05-2019, 11:52 AM
RE: Edit ordereddict - by Larz60+ - May-05-2019, 05:32 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Strange behavior list of list mmhmjanssen 3 1,617 May-09-2024, 11:32 AM
Last Post: mmhmjanssen
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 2,608 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 3,351 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 1,780 Feb-23-2023, 02:21 PM
Last Post: sparkt
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 3,426 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 3,087 Oct-01-2022, 07:15 PM
Last Post: Skaperen
Question Keyword to build list from list of objects? pfdjhfuys 3 2,624 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Split a number to list and list sum must be number sunny9495 5 3,830 Apr-28-2022, 09:32 AM
Last Post: Dexty
  How to check if a list is in another list finndude 4 9,464 Jan-17-2022, 05:04 PM
Last Post: bowlofred
  Different out when using conda list and pip list Led_Zeppelin 1 6,546 Jan-14-2022, 09:30 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

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