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
1,681 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,780 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, 2019 in Python by Wajiha
• 1,960 points
2,047 views
0 votes
1 answer

DataFrame slicing in Python

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

answered Jul 15, 2019 in Python by Sakra
766 views
0 votes
1 answer

How to reverse a string in Python using slicing operator?

Hi@akhtar, Strings can be reversed using slicing. To ...READ MORE

answered Aug 17, 2020 in Python by MD
• 95,460 points
3,765 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,890 points
20,009 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,070 views
+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
1,307 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
35,403 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP