Slicing in Python

+2 votes
Can someone tell me slicing is supposed to work in Python? It would be great if you could explain all the different ways slicing is used.
Apr 13, 2018 in Python by kaaala billi
40 views

6 answers to this question.

+1 vote

This is what you're looking for:

a[start:end] # items from (start) to (end - 1)
a[start:] # items from start through the rest of the array
a[:end] # all items until and excluding (end)
a[:] # a copy of the whole array
a[start:end:step] #extract array elements in steps of the specified interval.

The "step" parameter can be a negative number too, in which the list will be scanned backwards in steps of the given length.

answered Apr 13, 2018 by Nietzsche's daemon
• 4,260 points
+1 vote
One way to remember how slices work is to think of the indices as pointing betweencharacters, with the left edge of the first character numbered 0. 

Then the right edge of the last character of a string of n characters has index n

 +---+---+---+---+---+
 | H | e | l | p | A |
 +---+---+---+---+---+
 0   1   2   3   4   5
-5  -4  -3  -2  -1

The first row of numbers gives the position of the indices 0...5 in the string, 
the second row gives the corresponding negative indices.
answered Oct 18, 2018 by findingbugs
• 4,750 points
+1 vote

To understand slice assignment, it's helpful to add another concept to the ascii art:

                +---+---+---+---+---+---+
                | P | y | t | h | o | n |
                +---+---+---+---+---+---+
Slice position: 0   1   2   3   4   5   6
Index position:   0   1   2   3   4   5

>>> p = ['P','y','t','h','o','n']
# Why the two sets of numbers: 
# indexing gives items, not lists
>>> p[0]
 'P'
>>> p[5]
 'n'
# slicing gives lists
>>> p[0:1]
 ['P']
>>> p[0:2]
 ['P','y']
answered Oct 18, 2018 by Nabarupa
+1 vote

Slice notation works like this:

sequence[start:stop:step]
answered Oct 18, 2018 by Arihanth
+1 vote

Couple of things that weren't immediately obvious to me when I first saw the slicing syntax:

>>> x = [1,2,3,4,5,6]
>>> x[::-1]
[6,5,4,3,2,1]

Easy way to reverse sequences!

And if you wanted, for some reason, every second item in the reversed sequence:

>>> x = [1,2,3,4,5,6]
>>> x[::-2]
[6,4,2]
answered Oct 18, 2018 by Valarian
+1 vote
Index:
      ------------>
  0   1   2   3   4
+---+---+---+---+---+
| a | b | c | d | e |
+---+---+---+---+---+
  0  -4  -3  -2  -1
      <------------

Slice:
    <---------------|
|--------------->
:   1   2   3   4   :
+---+---+---+---+---+
| a | b | c | d | e |
+---+---+---+---+---+
:  -4  -3  -2  -1   :
|--------------->
    <---------------|

I hope this will help you to model the list in Python.

answered Oct 18, 2018 by abc

Related Questions In Python

0 votes
1 answer

Slicing a list using a variable, in Python

Yes its possible. Use the following piece ...READ MORE

answered Jul 2 in Python by Wajiha
• 1,120 points
16 views
0 votes
1 answer

DataFrame slicing in Python

You can use criteria based subset for ...READ MORE

answered 4 days ago in Python by Sakra
9 views
+3 votes
7 answers

How can I rename a file in Python?

yes, you can use "os.rename" for that. ...READ MORE

answered Mar 31, 2018 in Python by DareDev
• 6,810 points
89 views
+2 votes
2 answers

How can I create a new file in Python?

You can try the below code which ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,420 points
69 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 128 views
+4 votes
6 answers
+4 votes
6 answers

Substring search in Python

Use the "in" operator in python" if "substring" ...READ MORE

answered Apr 13, 2018 in Python by Nietzsche's daemon
• 4,260 points
76 views
+4 votes
7 answers

Splitting a List into chunks in Python

Here's a generator that yields the chunks ...READ MORE

answered Apr 13, 2018 in Python by Nietzsche's daemon
• 4,260 points
3,032 views