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
#1
I have a list of ordereddict as follows
OrderedDict([('Numbers', '15'), ('FirstName', 'John'), ('SecondName', 'Raul'), ('MiddleName', 'Kyle')])
OrderedDict([('Names', 'John'), ('NewFirstName', 'Mark'), ('NewSecondName', 'Sachel'), ('NewThirdName', 'Raul')])
i want to search whether the values of Names,NewFirstName,NewSecondName,NewThirdName is present in value of SecondName, if it exists it should return the corresponding Numbers value. How can i do this, is there any builtin function for it?
Reply
#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
#3
I have a list of ordereddict that look like below, these are already sorted

[OrderedDict([('Numbers', '15'), ('FirstName', 'John'), ('SecondName', 'Raul'), ('MiddleName', 'Kyle')]),
    OrderedDict([('Names', 'John'), ('NewFirstName', 'Mark'), ('NewSecondName', 'Sachel'), ('NewThirdName', 'Raul')]),
    OrderedDict([('Numbers', '25'),  ('FirstName', 'Kyle'), ('SecondName', 'Venn'), ('MiddleName', 'Marcus')]),
    OrderedDict([('Names', 'Sachel'), ('NewFirstName', 'Venn'), ('NewSecondName', 'Kyle'), ('NewThirdName', 'John')]),
    OrderedDict([('Names', 'Kyle'),  ('NewFirstName', 'Son'), ('NewSecondName', 'Son'), ('NewThirdName', 'Sachel')]),
    OrderedDict([('Numbers', '31'),  ('FirstName', 'Son'), ('SecondName', 'John'), ('MiddleName', 'John')]),
    OrderedDict([('Numbers', '43'),  ('FirstName', 'Marcus'), ('SecondName', 'Venn'), ('MiddleName', 'Venn')]),
    OrderedDict([('Numbers', '57'),  ('FirstName', 'Sachel'), ('SecondName', 'Sachel'), ('MiddleName', 'Raul')]),
    OrderedDict([('Names', 'Moore'),  ('NewFirstName', 'Sachel'), ('NewSecondName', 'Kyle'), ('NewThirdName', 'Raul')]),
    OrderedDict([('Names', 'Venn'),  ('NewFirstName', 'Mark'), ('NewSecondName', 'Kyle'), ('NewThirdName', 'Sachel')]),
    OrderedDict([('Names', 'Mark'),  ('NewFirstName', 'Mark'), ('NewSecondName', 'John'), ('NewThirdName', 'Mark')]),
    OrderedDict([('Numbers', '63'),  ('FirstName', 'Kyle'), ('SecondName', 'Marcus'), ('MiddleName', 'Mark')]),
    OrderedDict([('Names', 'Son'),  ('NewFirstName', 'Venn'), ('NewSecondName', 'Moore'), ('NewThirdName', 'Sachel')]),
    OrderedDict([('Numbers', '71`'),  ('FirstName', 'Son'), ('SecondName', 'John'), ('MiddleName', 'Moore')]),
    OrderedDict([('Names', 'Marcus'),  ('NewFirstName', 'Marcus'), ('NewSecondName', 'John'), ('NewThirdName', 'Sachel')]),
    OrderedDict([('Numbers', '88'),  ('FirstName', 'Marcus'), ('SecondName', 'Mark'), ('MiddleName', 'John')]),
    OrderedDict([('Names', 'Raul'),  ('NewFirstName', 'John'), ('NewSecondName', 'Venn'), ('NewThirdName', 'Moore')]),
    OrderedDict([('Numbers', '94'),  ('FirstName', 'John'), ('SecondName', 'Marcus'), ('MiddleName', 'Moore')]),
    OrderedDict([('Numbers', '101'),  ('FirstName', 'Kyle'), ('SecondName', 'Venn'), ('MiddleName', 'Mark')]),
    OrderedDict([('Names', 'Ellen'), ('NewFirstName', 'Raul'), ('NewSecondName', 'Venn'), ('NewThirdName', 'Sachel')])]
For each item that contains the Names key i want to take the value of it and check the existence of it in SecondName of the item which has Numbers key in it and append that as value to it.

**For Example:** First item did not have Names key, so i need to go to second line which has a Names key with the value John. Take that and search for value John in SecondName key in all the items that has Numbers key in it, here Numbers 31 and Numbers 71 has that. So the new list of ordereddict should have these values appended in it as follows

OrderedDict([('Names', 'John(31,71)'), ('NewFirstName', 'Mark'), ('NewSecondName', 'Sachel'), ('NewThirdName', 'Raul')])
if the John does not exist in any of the items that has Numbers key in it, John should simply be appended with the value of the Numbers key that exists before it with * appended

The above process should work for the NewFirstName, NewSecondName , NewThirdName as well with respect to SecondName

Mark exists as SecondName in Number 88 and Sachel exists as SecondName in Number 57 and Raul as 15 so the new list of ordereddict would contain this, the complete expected new list of ordereddict is as below


[OrderedDict([('Numbers', '15'), ('FirstName', 'John'), ('SecondName', 'Raul'), ('MiddleName', 'Kyle')]),
    OrderedDict([('Names', 'John(31,71)'), ('NewFirstName', 'Mark(88)'), ('NewSecondName', 'Sachel(57)'), ('NewThirdName', 'Raul(15)')]),
    OrderedDict([('Numbers', '25'),  ('FirstName', 'Kyle'), ('SecondName', 'Venn'), ('MiddleName', 'Marcus')]),
    OrderedDict([('Names', 'Sachel(57)'), ('NewFirstName', 'Venn(25,43,101)'), ('NewSecondName', 'Kyle(25*)'), ('NewThirdName', 'John(31,71)')]),
    OrderedDict([('Names', 'Kyle(25*)'),  ('NewFirstName', 'Son(63*)'), ('NewSecondName', 'Son(63*)'), ('NewThirdName', 'Sachel(57)')]),
    OrderedDict([('Numbers', '31'),  ('FirstName', 'Son'), ('SecondName', 'John'), ('MiddleName', 'John')]),
    OrderedDict([('Numbers', '43'),  ('FirstName', 'Marcus'), ('SecondName', 'Venn'), ('MiddleName', 'Venn')]),
    OrderedDict([('Numbers', '57'),  ('FirstName', 'Sachel'), ('SecondName', 'Sachel'), ('MiddleName', 'Raul')]),
    OrderedDict([('Names', 'Moore(57*)'),  ('NewFirstName', 'Sachel(57)'), ('NewSecondName', 'Kyle(25*)'), ('NewThirdName', 'Raul(15)']),
    OrderedDict([('Names', 'Venn(25,43,101)'),  ('NewFirstName', 'Mark(88)'), ('NewSecondName', 'Kyle(25*)'), ('NewThirdName', 'Sachel(57)')]),
    OrderedDict([('Names', 'Mark(88)'),  ('NewFirstName', 'Mark(88)'), ('NewSecondName', 'John(31,71)'), ('NewThirdName', 'Mark(88)')]),
    OrderedDict([('Numbers', '63'),  ('FirstName', 'Kyle'), ('SecondName', 'Marcus'), ('MiddleName', 'Mark')]),
    OrderedDict([('Names', 'Son(63*)'),  ('NewFirstName', 'Venn(25,43,101)'), ('NewSecondName', 'Moore(57*)'), ('NewThirdName', 'Sachel(57)')]),
    OrderedDict([('Numbers', '71`'),  ('FirstName', 'Son'), ('SecondName', 'John'), ('MiddleName', 'Moore')]),
    OrderedDict([('Names', 'Marcus(94)'),  ('NewFirstName', 'Marcus(94)'), ('NewSecondName', 'John(31,71)'), ('NewThirdName', 'Sachel(57)')]),
    OrderedDict([('Numbers', '88'),  ('FirstName', 'Marcus'), ('SecondName', 'Mark'), ('MiddleName', 'John')]),
    OrderedDict([('Names', 'Raul(15)'),  ('NewFirstName', 'John(31,71)'), ('NewSecondName', 'Venn(25,43,101)'), ('NewThirdName', 'Moore(57*)')]),
    OrderedDict([('Numbers', '94'),  ('FirstName', 'John'), ('SecondName', 'Marcus'), ('MiddleName', 'Moore')]),
    OrderedDict([('Numbers', '101'),  ('FirstName', 'Kyle'), ('SecondName', 'Venn'), ('MiddleName', 'Mark')]),
    OrderedDict([('Names', 'Ellen(101*)'), ('NewFirstName', 'Raul(15)'), ('NewSecondName', 'Venn(25,43,101)'), ('NewThirdName', 'Sachel(57)'])]
To achieve this, i started of with the following code, but i feel like going nowhere

    for i in list1:
        if 'Names' in i.keys():
            if 'John' in i.values():
                print(i.items())
Please let me know the right pythonic way of achieving the expected result
Reply
#4
This is really a continuation of your previous thread, and should have been added to that thread.
This might squeeze by as a new topic, but very iffy.
In the future, please know that this is against forum rules.
Reply
#5
(May-02-2019, 04:41 PM)Larz60+ Wrote: This is really a continuation of your previous thread, and should have been added to that thread.
This might squeeze by as a new topic, but very iffy.
In the future, please know that this is against forum rules.

Oops, sorry for that. Will take a note of it
Reply
#6
Can someone help me on this please
Reply
#7
I have a list of ordereddict as follows

   list1= [OrderedDict([('Numbers', '15'), ('FirstName', 'John'), ('SecondName', 'Raul'), ('MiddleName', 'Kyle')]),
    OrderedDict([('Names', 'John'), ('NewFirstName', 'Mark'), ('NewSecondName', 'Sachel'), ('NewThirdName', 'Raul')]),
    OrderedDict([('Numbers', '25'),  ('FirstName', 'Kyle'), ('SecondName', 'Venn'), ('MiddleName', 'Marcus')]),
    OrderedDict([('Names', 'Sachel'), ('NewFirstName', 'Venn'), ('NewSecondName', 'Kyle'), ('NewThirdName', 'John')])]
I would like to edit some of the values in it by picking the value from another list

    list2= [('John', ['1023', '1235']), ('Venn', ['618']), ('Marcus', ['909']), ('Kyle', ['109', '1233', '987']), ('Raul', '5*'), ('Sachel', '10*')]


Expected Output:

From list2 i need to pick the value of John i.e 1023,1235 and paste that next to John in list1 wherever present , so the list1 would become

    list1= [OrderedDict([('Numbers', '15'), ('FirstName', 'John(1023,1235)'), ('SecondName', 'Raul'), ('MiddleName', 'Kyle')]),
    OrderedDict([('Names', 'John(1023,1235)'), ('NewFirstName', 'Mark'), ('NewSecondName', 'Sachel'), ('NewThirdName', 'Raul')]),
    OrderedDict([('Numbers', '25'),  ('FirstName', 'Kyle'), ('SecondName', 'Venn'), ('MiddleName', 'Marcus')]),
    OrderedDict([('Names', 'Sachel'), ('NewFirstName', 'Venn'), ('NewSecondName', 'Kyle'), ('NewThirdName', 'John(1023,1235)')])]
Reply
#8
Next time you post a new thread for a topic that you already have a running thread for, you will get a warning.
I have mentioned this before.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  No matter what I do I get back "List indices must be integers or slices, not list" Radical 4 1,154 Sep-24-2023, 05:03 AM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,507 May-01-2023, 09:06 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 911 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 1,826 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,610 Oct-01-2022, 07:15 PM
Last Post: Skaperen
Question Keyword to build list from list of objects? pfdjhfuys 3 1,550 Aug-06-2022, 11:39 PM
Last Post: Pedroski55
  Split a number to list and list sum must be number sunny9495 5 2,275 Apr-28-2022, 09:32 AM
Last Post: Dexty
  How to check if a list is in another list finndude 4 1,831 Jan-17-2022, 05:04 PM
Last Post: bowlofred
  Different out when using conda list and pip list Led_Zeppelin 1 4,015 Jan-14-2022, 09:30 PM
Last Post: snippsat
  Use one list as search key for another list with sublist of list jc4d 4 2,147 Jan-11-2022, 12:10 PM
Last Post: jc4d

Forum Jump:

User Panel Messages

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