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
150
votes
4 answers

Declare slice or make slice?

In Go, what is the difference between var s []int and s := make([]int, 0)? I find that both works, but which one is better?
Wang Yi
  • 1,777
  • 3
  • 13
  • 6
143
votes
5 answers

Extract elements of list at odd positions

So I want to create a list which is a sublist of some existing list. For example, L = [1, 2, 3, 4, 5, 6, 7], I want to create a sublist li such that li contains all the elements in L at odd positions. While I can do it by L = [1, 2, 3, 4, 5, 6,…
veepsk
  • 1,703
  • 3
  • 14
  • 21
142
votes
8 answers

How to search for an element in a golang slice

I have a slice of structs. type Config struct { Key string Value string } // I form a slice of the above struct var myconfig []Config // unmarshal a response body into the above slice if err := json.Unmarshal(respbody, &myconfig); err !=…
codec
  • 7,978
  • 26
  • 71
  • 127
142
votes
8 answers

Pandas selecting by label sometimes return Series, sometimes returns DataFrame

In Pandas, when I select a label that only has one entry in the index I get back a Series, but when I select an entry that has more then one entry I get back a data frame. Why is that? Is there a way to ensure I always get back a data frame? In…
Job Evers
  • 4,077
  • 5
  • 20
  • 26
139
votes
5 answers

Implementing slicing in __getitem__

I am trying to implement slice functionality for a class I am making that creates a vector representation. I have this code so far, which I believe will properly implement the slice but whenever I do something like v[4] where v is a vector, python…
nicotine
  • 2,519
  • 3
  • 19
  • 15
139
votes
4 answers

range over interface{} which stores a slice

Given the scenario where you have a function which accepts t interface{}. If it is determined that the t is a slice, how do I range over that slice? func main() { data := []string{"one","two","three"} test(data) moredata := []int{1,2,3} …
Owen Allen
  • 11,348
  • 9
  • 51
  • 63
137
votes
11 answers

How to find out element position in slice?

How does one determine the position of an element present in slice? I need something like the following: type intSlice []int func (slice intSlice) pos(value int) int { for p, v := range slice { if (v == value) { return p …
OCyril
  • 2,875
  • 4
  • 20
  • 11
135
votes
5 answers

How does assignment work with list slices?

Python docs says that slicing a list returns a new list. Now if a "new" list is being returned I've the following questions related to "Assignment to slices" a = [1, 2, 3] a[0:2] = [4, 5] print a Now the output would be: [4, 5, 3] How can…
Kartik Anand
  • 4,513
  • 5
  • 41
  • 72
131
votes
3 answers

How do I check for an empty slice?

I am calling a function that returns an empty array if there are no values. When I do this it doesn't work: if r == [] { fmt.Println("No return value") } The work around I'm using is: var a [0]int if r == a { fmt.Println("No…
Kshitiz Sharma
  • 17,947
  • 26
  • 98
  • 169
127
votes
5 answers

Are slices passed by value?

In Go, I am trying to make a scramble slice function for my traveling salesman problem. While doing this I noticed when I started editing the slice I gave the scramble function was different every time I passed it in. After some debugging I found…
duck
  • 1,674
  • 2
  • 16
  • 26
122
votes
10 answers

Pairs from single list

Often enough, I've found the need to process a list by pairs. I was wondering which would be the pythonic and efficient way to do it, and found this on Google: pairs = zip(t[::2], t[1::2]) I thought that was pythonic enough, but after a recent…
Apalala
  • 9,017
  • 3
  • 30
  • 48
120
votes
6 answers

What does [:] mean?

I'm analyzing some Python code and I don't know what pop = population[:] means. Is it something like array lists in Java or like a bi-dimensional array?
andriy
  • 4,074
  • 9
  • 44
  • 71
113
votes
1 answer

How to convert a &str to a &[u8]

This seems trivial, but I cannot find a way to do it. For example, fn f(s: &[u8]) {} pub fn main() { let x = "a"; f(x) } Fails to compile with: error: mismatched types: expected `&[u8]`, found `&str` (expected slice, found str)…
ynimous
  • 4,642
  • 6
  • 27
  • 43
109
votes
5 answers

Slicing a list in Python without generating a copy

I have the following problem. Given a list of integers L, I need to generate all of the sublists L[k:] for k in [0, len(L) - 1], without generating copies. How do I accomplish this in Python? With a buffer object somehow?
Chris
  • 3,109
  • 7
  • 29
  • 39
99
votes
9 answers

Why does reversing a list using slice notation with 0 as "stop" not return the entire list?

In the following example: foo = ['red', 'white', 'blue', 1, 2, 3] foo[0:6:1] will print all elements in foo. However, foo[6:0:i-1] will omit the 1st or 0th element. >>> foo[6:0:-1] [3, 2, 1, 'blue', 'white'] I understand that I can use…
user737079
  • 993
  • 1
  • 7
  • 4