I have the following list of lists:
MyList = [[130, 10], [131, 15], [132, 1]]
Then i have some inputs. If i get, for example: Data = [130, 3], [135, 10], i need to update the list like this:
MyList = [[130, 3], [131, 15], [132, 1], [135, 10]]
So, if in MyList there is already a sublist where the first element is the same as the first element of a sublist in Data, update the same element. Instead, if there isn't one, append it.
I managed to do this but i was wondering if there was a cleaner solution, as i really don't like the actual one:
Temp = [x[0] for x in MyList]
for x in Data:
if x[0] not in Temp:
Sublist = []
Sublist.append(x[0])
Sublist.append(x[1])
MyList.append(Sublist)
else:
for y in MyList:
if x[0] == y[0]:
x[1] = y[1]
Is there any better way to do this? I feel like this code can be improved, i also don't like editing elements while looping. Any kind of help is welcome!
dictwas usedlist, probably better of with a dict.