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