If someone knows how to phrase the question to make it clearer, feel free to edit it!
I have a list like this:
a = [["Adolf", "10"], ["Hermann", "20"], ["Heinrich", "30"]]
and I have an 'update' list like this:
b = [["Rudolf", "40"], ["Adolf", "50"]]
I want to be able to add new entries and overwrite identical entries in list a
using list b
:
c = magic(a, b)
The result should be as follows:
>>> print(c)
[["Adolf", "50"], ["Hermann", "20"], ["Heinrich", "30"], ["Rudolf", "40"]]
Is there a function like magic in existence? If not, how could it be written?
EDIT: Just to be clear, I am aware of the dictionary approach. For my purposes, I need to have duplicate entries, so dictionaries are not appropriate -- that's why I asked about lists. Before it is mentioned, there'll be protections for these special duplicate entries. For example, let's say "Heinrich" is one of these special types of entry that can have duplicates:
a = [['Adolf', '10'], ['Hermann', '20'], ['Heinrich', '30'], ['Heinrich', '15']]
Now, let's say I have the update list as the following:
b = [['Rudolf', '40'], ['Adolf', '50']]
Updating list a
with list b
should result in the following list:
>>> print(c)
[['Adolf', '50'], ['Hermann', '20'], ['Heinrich', '30'], ['Heinrich', '15'], ['Rudolf', '40']]
As you can see, there are intended duplicate entries. This is why a dictionary cannot be used directly.