What is the python code equivalent of slicing a list in dart other than using the SUBLIST method?

Question:

I understand that the sublist method works similar to slicing, but what are some other ways i can get similar outputs?

For example

List = [1,3,5,7,9]

Python code
ListSliced = List[1:]

Using sublist in dart
ListSliced = List.sublist(1);

Is there a equivalent of List[1:] in DART??

Code written in dart.

List<dynamic>' is not a subtype of type 'bool'

branches(tree) {
  return tree.sublist(1);
}

isLeaf(tree) {
  return (!branches(tree));
}

Equivalent code in python.

def branches(tree):
        return tree[1:]

def is_leaf(tree):
        return not branches(tree)

Any ideas on whats causing this error

Full code for the tree program.


bool isTree(tree) {
  if ((tree is! List) | (tree.length < 1)) {
    return false;
  }
  for (final branch in branches(tree)) {
    if (!isTree(branch)) {
      return false;
    }
  }
  return true;
}

branches(tree) {
  return tree.sublist(1);
}

Object label(tree) {
  return tree[0];
}

List tree(rootLabel, [List branches = const []]) {
  for (final branch in branches) {
    assert(isTree(branch));
  }
  return ([rootLabel] + branches);
}

isLeaf(tree) {
  return (!branches(tree));
}

var t = tree('hey', [
  tree('hello'),
  tree('hum', [tree('there'), tree('hey')])
]);

Asked By: zram

||

Answers:

Another way is to use skip

For example:

List l = [1,3,5,7,9];
print(l.skip(2)); //prints (5,7,9)
Answered By: Ivo
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.