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 1,084 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.

• 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.```
• 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

## Slicing a list using a variable, in Python

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

## DataFrame slicing in Python

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

## How to reverse a string in Python using slicing operator?

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

## How can I rename a file in Python?

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

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote

## how can i count the items in a list?

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

## Lowercase in Python

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