Skip to main content
The 2026 Annual Developer Survey is live— take the Survey today!
Shorten footer meta commentary.
Source Link
Mateen Ulhaq

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 Python's slice and range upper-bound exclusive? to learn why xs[0:2] == [xs[0], xs[1]], i.e. no not xs[2][..., xs[2]].
See Make a new list containing every Nth item in the original list for xs[::N].
See How does assignment work with list slices? to learn what xs[0:2] = [4["a", 5]"b"] does.

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 Python's slice and range upper-bound exclusive? to learn why xs[0:2] == [xs[0], xs[1]], i.e. no xs[2].
See Make a new list containing every Nth item in the original list for xs[::N].
See How does assignment work with list slices? to learn what xs[0:2] = [4, 5] does.

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?


See Why are Python's slice and range upper-bound exclusive? to learn why xs[0:2] == [xs[0], xs[1]], not [..., xs[2]].
See Make a new list containing every Nth item in the original list for xs[::N].
See How does assignment work with list slices? to learn what xs[0:2] = ["a", "b"] does.

Shorten footer meta commentary.
Source Link
Mateen Ulhaq

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 Python's slice and range upper-bound exclusive? for more discussion of the design decisions behind the notationto learn why xs[0:2] == [xs[0], xs[1]], i.e. no xs[2].

  
See Make a new list containing every Nth item in the original list for the most common practical usage of slicing (and other ways to solve the problem): getting every Nth element of a list. Please use that question instead as a duplicate target where appropriatexs[::N].

  
For more specific answers about slice assignment, seeSee How does assignment work with list slices? to learn what (although this is also addressed here)xs[0:2] = [4, 5] does.

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 Python's slice and range upper-bound exclusive? for more discussion of the design decisions behind the notation.

 See Make a new list containing every Nth item in the original list for the most common practical usage of slicing (and other ways to solve the problem): getting every Nth element of a list. Please use that question instead as a duplicate target where appropriate.

 For more specific answers about slice assignment, see How does assignment work with list slices? (although this is also addressed here).

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 Python's slice and range upper-bound exclusive? to learn why xs[0:2] == [xs[0], xs[1]], i.e. no xs[2]. 
See Make a new list containing every Nth item in the original list for xs[::N]. 
See How does assignment work with list slices? to learn what xs[0:2] = [4, 5] does.

Active reading [<https://en.wikipedia.org/wiki/Python_%28programming_language%29>].
Source Link
Peter Mortensen

How slicing in pythonPython works

How slicing in pythonPython 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 Python's slice and range upper-bound exclusive? for more discussion of the design decisions behind the notation.

See Make a new list containing every Nth item in the original list for the most common practical usage of slicing (and other ways to solve the problem): getting every Nth element of a list. Please use that question instead as a duplicate target where appropriate.

For more specific answers about slice assignment, see How does assignment work with list slices? (although this is also addressed here).

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 Python's slice and range upper-bound exclusive? for more discussion of the design decisions behind the notation.

See Make a new list containing every Nth item in the original list for the most common practical usage of slicing (and other ways to solve the problem): getting every Nth element of a list. Please use that question instead as a duplicate target where appropriate.

For more specific answers about slice assignment, see How does assignment work with list slices? (although this is also addressed here).

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 Python's slice and range upper-bound exclusive? for more discussion of the design decisions behind the notation.

See Make a new list containing every Nth item in the original list for the most common practical usage of slicing (and other ways to solve the problem): getting every Nth element of a list. Please use that question instead as a duplicate target where appropriate.

For more specific answers about slice assignment, see How does assignment work with list slices? (although this is also addressed here).

Fixed the title so people can search for it easily.
Link
Benny Frog
Loading
added 155 characters in body
Source Link
Karl Knechtel
Loading
enhance see-also section, including closure guidance
Source Link
Karl Knechtel
Loading
ask explicitly; add see-also section; restore a bit of OP's original phrasing, since there was nothing wrong with that part
Source Link
Karl Knechtel
Loading
Shorten.
Source Link
Mateen Ulhaq
Loading
deleted 8 characters in body; edited tags
Source Link
Géry Ogam
Loading
edited tags
Link
kmario23
Loading
Edited question title and removed language tag.
Link
martineau
Loading
edited title
Link
codeforester
Loading
edited title
Link
coldspeed95
Loading
Edited question title and removed language tag.
Link
martineau
Loading
Rollback to Revision 7
Link
Michał Perłakowski
Loading
Edited question title and removed language tag.
Link
martineau
Loading
deleted 23 characters in body
Source Link
Aaron Hall
Loading
edited tags
Link
thefourtheye
Loading
added 20 characters in body; edited tags; edited title
Source Link
Aaron Hall
Loading
deleted 1 characters in body; edited title
Source Link
arshajii
Loading
Question Protected by Jon Clements
deleted 66 characters in body; edited title
Source Link
Peter O.
Loading
Chet
Loading
Source Link
Simon
Loading
lang-py