Reputation: 1819
I looking for an on-the-shelf way to convert a List into a Map in Dart.
In python for example you can do:
l= [ ('a',(1,2)), ('b',(2,3)), ('c',(3,4) ) ]
d=dict(l)
==> {'a': (1, 2), 'c': (3, 4), 'b': (2, 3)}
The dict function expects a List of couple. For each couple, the first element is used as the key and the second as the data.
In Dart I saw the following method for a List : asMap(), but it's not doing what i expect: it use the list index as key. My questions:
Proposal:
List.toMap() //same as python dict.
List.toMap( (value) => [ value[0], value[1] ] ) //Using anonymous function to return a key and a value from a list item.
Thanks and Regards,
Nicolas
Upvotes: 120
Views: 114796
Reputation: 603
List list = [Gum, 0.95, Soda, 1.05, Chips, 1.99];
Map map1 = {};
for (int i = 0; i < list.length ; i += 2) {
map1[list[i]] = list[i + 1]; }
print(map1);
The output will be:
{Gum: 0.95, Soda: 1.05, Chips: 1.99}
Upvotes: 0
Reputation: 927
this is how you can convert list into map
void main() {
var list = [99, 25, 45, 11, 5, 125, 50];
SplayTreeMap numbers = new SplayTreeMap<int, int>();
numbers.addAll(list.asMap());
print(numbers);
}
Upvotes: 0
Reputation: 7269
There are many solutions, but the simplest are :
1- Use the asMap()
function
List list = ['steve', 'bill', 'musk'];
Map map = list.asMap();
print(map);
Output :
{0: steve, 1: bill, 2: musk}
2- Use for-in
collection inside your map Map map = {};
List list = ['steve', 'bill', 'musk'];
Map map = {for (var item in list) '$item' : 'valueOf$item'};
print(map);
Output :
{steve: valueOfsteve, bill: valueOfbill, musk: valueOfmusk}
Hope it will be helpful .
Upvotes: 69
Reputation: 3307
This may not directly relevant to the answer but, if there is anyone who wants to convert.
var list = ['fee','fi','fo','fum'];
to a Map like,
var map = {0:'fee', 1:'fi', 2:'fo', 3:'fum'};
you can use
var map = list.asMap();
PS
This map is not editable
Upvotes: 4
Reputation: 25294
/libs/extensions/list.dart
extension MapFromList<Element> on List<Element> {
Map<Key, Element> toMap<Key>(
MapEntry<Key, Element> Function(Element e) getEntry) =>
Map.fromEntries(map(getEntry));
Map<Key, Element> toMapWhereKey<Key>(Key Function(Element e) getKey) =>
Map.fromEntries(map((e) => MapEntry(getKey(e), e)));
}
import 'package:myApp/libs/extensions/list.dart';
final list = [1,2,3,4];
print(list.toMap((e) => MapEntry(e-1, e))); // {0: 1, 1: 2, 2: 3, 3: 4}
print(list.toMapWhereKey((e) => "str $e")); // {str 1: 1, str 2: 2, str 3: 3, str 4: 4}
Upvotes: 4
Reputation: 19617
Another possibility is to use the Map.fromEntries()
constructor:
final result = Map.fromEntries(l.map((value) => MapEntry(value[0], value[1])));
Upvotes: 27
Reputation: 76183
You can use Map.fromIterable:
var result = Map.fromIterable(l, key: (v) => v[0], value: (v) => v[1]);
or collection-for (starting from Dart 2.3):
var result = { for (var v in l) v[0]: v[1] };
Upvotes: 199
Reputation: 786
In Dart 1.1, you can use this constructor of Map:
new Map.fromIterable(list, key: (v) => v[0], value: (v) => v[1]);
This would be closest to your original proposal.
Upvotes: 71
Reputation: 1180
There is not yet a concept of tuples or pairs in dart (other than a two element Iterable). If Dart had such a concept, then issue 7088 proposes to solve this with a Map.fromPairs constructor.
Upvotes: 0