Questions tagged [slice]

A slice is a representation of a part of a sequence, usually defined by a reference to the underlying sequence, an index giving the starting position, a length or end position, and optionally a "stride" or "step" value. Please use the tags "object-slicing" for the slicing problem in C++ and "program-slicing" for the analysis technique.

A slice is a representation of a part of a sequence (including, but not limited to, lists, arrays and strings), usually defined by a reference to the underlying sequence, an index giving the starting position, a length or end position, and optionally a "stride" or "step" value.

Further reading:

6208 questions
4512
votes
38 answers

How slicing in Python works

How does Python's slice notation work? That is: when I write code like a[x:y:z], a[:], a[::2] etc., how can I understand which elements end up in the slice? Please include references where appropriate. See Why are slice and range upper-bound…
Simon
  • 78,655
  • 25
  • 88
  • 118
2505
votes
25 answers

How do I chop/slice/trim off last character in string using Javascript?

I have a string, 12345.00, and I would like it to return 12345.0. I have looked at trim, but it looks like it is only trimming whitespace and slice which I don't see how this would work. Any suggestions?
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
1151
votes
8 answers

What is the difference between String.slice and String.substring?

Does anyone know what the difference is between these two methods? String.prototype.slice String.prototype.substring
tmim
  • 12,091
  • 4
  • 18
  • 12
1093
votes
13 answers

How to get first N number of elements from an array

I am working with Javascript(ES6) /FaceBook react and trying to get the first 3 elements of an array that varies in size. I would like do the equivalent of Linq take(n). In my Jsx file I have the following: var items = list.map(i => { return ( …
user1526912
  • 15,818
  • 14
  • 57
  • 92
803
votes
9 answers

Concatenate two slices in Go

I'm trying to combine the slice [1, 2] and the slice [3, 4]. How can I do this in Go? I tried: append([]int{1,2}, []int{3,4}) but got: cannot use []int literal (type []int) as type int in append However, the documentation seems to indicate this is…
Kevin Burke
  • 61,194
  • 76
  • 188
  • 305
786
votes
29 answers

Remove last item from array

I have the following array. var arr = [1,0,2]; I would like to remove the last element i.e. 2. I used arr.slice(-1); but it doesn't remove the value.
Prithviraj Mitra
  • 11,002
  • 13
  • 58
  • 99
769
votes
25 answers

Fastest way to duplicate an array in JavaScript - slice vs. 'for' loop

In order to duplicate an array in JavaScript: Which of the following is faster to use? Slice method var dup_array = original_array.slice(); For loop for(var i = 0, len = original_array.length; i < len; ++i) dup_array[i] = original_array[i]; I…
Marco Demaio
  • 33,578
  • 33
  • 128
  • 159
763
votes
9 answers

Is there a foreach loop in Go?

Is there a foreach construct in the Go language? Can I iterate over a slice or array using a for?
tatsuhirosatou
  • 25,149
  • 14
  • 39
  • 40
446
votes
19 answers

Contains method for a slice

Is there anything similar to a slice.contains(object) method in Go without having to do a search through each element in a slice?
vosmith
  • 4,948
  • 2
  • 18
  • 26
434
votes
13 answers

Checking the equality of two slices

How can I check if two slices are equal, given that the operators == and != are not an option? package main import "fmt" func main() { s1 := []int{1, 2} s2 := []int{1, 2} fmt.Println(s1 == s2) } This does not compile with: invalid…
wei2912
  • 6,141
  • 2
  • 19
  • 20
428
votes
5 answers

How to get last items of a list in Python?

I need the last 9 numbers of a list and I'm sure there is a way to do it with slicing, but I can't seem to get it. I can get the first 9 like this: num_list[0:9]
Nope
  • 34,682
  • 42
  • 94
  • 119
410
votes
10 answers

Getting a slice of keys from a map

Is there any simpler/nicer way of getting a slice of keys from a map in Go? Currently I am iterating over the map and copying the keys to a slice: i := 0 keys := make([]int, len(mymap)) for k := range mymap { keys[i] = k i++ }
Saswat Padhi
  • 6,044
  • 4
  • 20
  • 25
409
votes
5 answers

Correct way to initialize empty slice

To declare an empty slice, with a non-fixed size, is it better to do: mySlice1 := make([]int, 0) or: mySlice2 := []int{} Just wondering which one is the correct way.
eouti
  • 5,338
  • 3
  • 34
  • 42
389
votes
11 answers

What is :: (double colon) in Python when subscripting sequences?

I know that I can use something like string[3:4] to get a substring in Python, but what does the 3 mean in somesequence[::3]?
Aillyn
  • 23,354
  • 24
  • 59
  • 84
324
votes
23 answers

How to delete an element from a Slice in Golang

fmt.Println("Enter position to delete::") fmt.Scanln(&pos) new_arr := make([]int, (len(arr) - 1)) k := 0 for i := 0; i < (len(arr) - 1); { if i != pos { new_arr[i] = arr[k] k++ i++ } else { k++ } } for i…
Anchal Sarraf
  • 3,443
  • 2
  • 14
  • 13
1
2 3
99 100