Good day.
Check, I have a
lista = {"A" : 100, "B" : 80, "C": 70, "D": 60, "E": 50, "F": 30}
What I want is to get a Map
with the first 3 elements:
listaFinal = {"A" : 100, "B" : 80, "C": 70}
Can I only create mi listaFinal
by making a to it for
or is there another way to get the listaFinal
?
Additionally, there will be some way to add its elements without doing an for
element loop x
.
I mean, get something like this
MapFinal2 = {"A" : 100, "B" : 80, "C": 70, "OTROS": 140}
Where "OTHERS" has the sum of "E", "F" and "G"
This code I am using to add:
var sum = lista.entries
//.where((e) => e.key.startsWith('A'))
.map<int>((e) => e.value)
.reduce((a, b) => a + b);
But in the Where I don't know what to put so that I only add the first 3 or 4 elements of the Map. Additionally I would like to cut my first map and get only the first 3.
Thank you
For the first case:
For the second case, as I mentioned, a simple for has O(n) complexity, so it doesn't affect anything.