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

Time:09-21

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')])
]);

CodePudding user response:

Another way is to use skip

For example:

List l = [1,3,5,7,9];
print(l.skip(2)); //prints (5,7,9)
  • Related