flutter
flutter

Reputation: 6776

Insert list items at start of of each item in a list of lists

I have two lists that are the same length:

List<List<String>> list1 = [["John","Omar","Jane"],["Rick","Hulie","Frank"],["Pri","Mary","Tim"]]
 List<int> list2 = [1,5,9]

I want to add insert the numbers from list2 to the start of the lists in list1 like this..

list3 to look like

 [["1","John","Omar","Jane"],["5", "Rick","Hulie","Frank"],["9","Pri","Mary","Tim"]]

Upvotes: 0

Views: 97

Answers (1)

Zeeshan Hussain
Zeeshan Hussain

Reputation: 842

You can loop through the list and insert each of the numbers at index 0.

 List<List<String>> list1 = [["John","Omar","Jane"],["Rick","Hulie","Frank"], ["Pri","Mary","Tim"]];
 List<int> list2 = [1,5,9];
 for (int i = 0;i < list1.length;i++) {
   list1[i].insert(0,list2[i].toString());
 }
 
 // if you want a more dart like the solution then use this.
 list1.asMap().forEach((index,value) => value.insert(0,list2[index].toString()));

Upvotes: 1

Related Questions