How to merge 2 lists in Dart?

11,990

Solution 1

I don't know anything ready-to-use, but this should do what you want:

import 'dart:math' as math;

void main() {
  List<String> array1 = ["John", "Bob", "Fred", "June", "Tom"];
  List<String> array2 = ["House", "Flat", "Bungalow"];

  List<String> output = List<String>(array1.length + array2.length);
  int i = 0;
  for (i; i < math.min(array1.length, array2.length); i++) {
    output[i * 2] = array1[i];
    output[i * 2 + 1] = array2[i];
  }
  print(output);
  if (array1.length != array2.length) {
    if (array1.length > array2.length) {
      output.setRange(i * 2, output.length, array1.sublist(i));
    } else {
      output.setRange(i * 2, output.length, array2.sublist(i));
    }
  }
  print(output);
}

Solution 2

i think this should work too

void main() {
  var list1 = ["John", "Bob", "Fred", "June", "Tom"];
  var list2 = ["House", "Flat", "Bungalow"];
  var list3 = [...list1, ...list2];

  print(list1);
  print(list2);
  print(list3);
}

the outputs should be like this (tested in DartPad)

[John, Bob, Fred, June, Tom]
[House, Flat, Bungalow]
[John, Bob, Fred, June, Tom, House, Flat, Bungalow]

Solution 3

Another solution is the following which is likely less efficient than Gunter's solution but is a bit more functional in nature and I like having multiple perspectives on how to solve a problem. If the arrays are as short as these, optimization on the join is likely not a concern anyways.

This solution creates an iterable on the indicies of the longest array and for each index i, creates potentially two entries from the input arrays if i is in both. Otherwise it includes only the element from the array which contains i.

import 'dart:math' as math;

main() {
  List<String> array1 = ["John", "Bob", "Fred", "June", "Tom"];
  List<String> array2 = ["House", "Flat", "Bungalow"];

  //["John", "House", "Bob", "Flat", "Fred", "Bungalow", "June", "Tom"]
  List<String> output =
      Iterable.generate(math.max(array1.length, array2.length))
          .expand((i) sync* {
    if (i < array1.length) yield array1[i];
    if (i < array2.length) yield array2[i];
  }).toList();

  print(output);
}

Solution 4

I doubt there is a core method that does exactly want you want. Take a look at my merge function:

import 'dart:math';

List<String> merge(List<String> a, List<String> b) {
    List<String> output = [];

    var min_length = min(a.length, b.length);
    var max_length = max(a.length, b.length);

    for(var i = 0; i < min_length; i++) {
      output.add(a[i]);
      output.add(b[i]);
    }

    List<String> longer = a.length > b.length ? a : b;

    for(var i = min_length; i < max_length; i++) {
      output.add(longer[i]);
    }

    return output;
  }

void main() {
    merge(
      ["John", "Bob", "Fred", "June", "Tom"],
      ["House", "Flat", "Bungalow"]
    ).forEach((e) => print(e));

    print('------');

    merge(
      ["John", "Bob"],
      ["House", "Flat", "Bungalow"]
    ).forEach((e) => print(e));
}

Solution 5

You could use addAll() method as in .

List<int> a =[1,2];  List<int> b =[1,2];
List<int> c =[];
c.addAll(a);
c.addAll(b);

Or insertAll(index,Iterateable<>)

List<int> a =[1,2];  
List<int> b =[1,2];
a.insertAll(a.length,b);

   
Share:
11,990

Related videos on Youtube

Jordan Davies
Author by

Jordan Davies

Updated on June 04, 2022

Comments

  • Jordan Davies
    Jordan Davies almost 2 years

    I'm trying to merge 2 lists so the output is the following:

    List<String> array1 = ["John", "Bob", "Fred", "June", "Tom"];
    List<String> array2 = ["House", "Flat", "Bungalow"];
    
    List<String> output = //["John", "House", "Bob", "Flat", "Fred", "Bungalow", "June", "Tom"]
    

    How can I achieve this? I've had a look at the zip function in the quiver library but it only returns a list as long as the shortest list that's passed into it.

    EDIT: To clarify, I essentially want to do the same as the zip function but if one array is longer than the other, I just want to append the left over items on to the end.

  • Jordan Davies
    Jordan Davies about 5 years
    Thanks for you answer, but won't this just append one array onto the other? I've updated the question to hopefully add more detail.
  • Günter Zöchbauer
    Günter Zöchbauer about 5 years
    So the order is relevant? I thought you just don't want duplicates.
  • Adam Cooper
    Adam Cooper over 4 years
    This simply appends one list onto the other, that's not what the questioner is asking. They want to merge the two.
  • Aly Yasser Pranajaya
    Aly Yasser Pranajaya over 4 years
    sorry? i create a new list and merge the other 2 using spread operator, why is that weren't counted as merging? also. i'd better use list.addAll(anotherList) to append a list.
  • Aly Yasser Pranajaya
    Aly Yasser Pranajaya over 4 years
    ah, my bad. my answer was, indeed didn't answer the question, sorry.
  • Adam Cooper
    Adam Cooper over 4 years
    No problem it's great that you got involved and suggested an answer. I'd suggest you rewrite or delete your answer though to avoid downvotes.