Ad

Flutter: Operation In List Of List

- 1 answer

I love python, I need to work on an app (so I can't work with python XD) I'm using Flutter and I need to do simple efficient operation in List.

I have a list of list and I need to get the first item of every sublist and create a new list out of it.

In python it's simple, beautiful and compact (oh boy I love python :) !).

table_2list = [
    [
        13,
        "oiseau"
    ],
    [
        6,
        "turbo"
    ],
    [
        819,
        "bateau"
    ],
]

nbr_list = [nbr for (nbr,label) in table_2list]
print(nbr_list)

result: [13, 6, 819]

How to do it in Flutter ? I looked for 1 line code but it seems we can't be as simple and easy to read as python ? (Yes I'm biased and I'm open to join your side and defend the Flutter banner too if you convince me with answers, explanation and/or links ! :D)

For the moment, my kingdom is Python, my king is simplicity and my god is readable !

Thank you in advance my future brothers !

Ad

Answer

final table_2list = [
  [13, "oiseau"],
  [6, "turbo"],
  [819, "bateau"],
];
void main(List<String> args) {
  final result = [for (var i in table_2list) i.first];
  print(result);
}

Ad
source: stackoverflow.com
Ad