Extract first item of each sublist in Python
Using list comprehension: >>> lst = [[‘a’,’b’,’c’], [1,2,3], [‘x’,’y’,’z’]] >>> lst2 = [item[0] for item in lst] >>> lst2 [‘a’, 1, ‘x’]
Using list comprehension: >>> lst = [[‘a’,’b’,’c’], [1,2,3], [‘x’,’y’,’z’]] >>> lst2 = [item[0] for item in lst] >>> lst2 [‘a’, 1, ‘x’]
In list[first:last], last is not included. The 10th element is ls[9], in ls[0:10] there isn’t ls[10].
subList returns a view on an existing list. It’s not an ArrayList. You can create a copy of it: sibling.keys = new ArrayList<Integer>(keys.subList(mid, this.num)); Or if you’re happy with the view behaviour, try to change the type of sibling.keys to just be List<Integer> instead of ArrayList<Integer>, so that you don’t need to make the copy: … Read more
You want List::GetRange(firstIndex, count). // I have a List called list List sublist = list.GetRange(5, 5); // (gets elements 5,6,7,8,9) List anotherSublist = list.GetRange(0, 4); // gets elements 0,1,2,3) Is that what you’re after? If you’re looking to delete the sublist items from the original list, you can then do: // list is our original … Read more