Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
Shorten footer meta commentary.
Source Link
Mateen Ulhaq
  • 27.9k
  • 21
  • 121
  • 155

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
  • 27.9k
  • 21
  • 121
  • 155

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
  • 31.4k
  • 22
  • 110
  • 134

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).

Loading
added 155 characters in body
Source Link
Karl Knechtel
  • 61.4k
  • 14
  • 133
  • 193
Loading
enhance see-also section, including closure guidance
Source Link
Karl Knechtel
  • 61.4k
  • 14
  • 133
  • 193
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
  • 61.4k
  • 14
  • 133
  • 193
Loading
Shorten.
Source Link
Mateen Ulhaq
  • 27.9k
  • 21
  • 121
  • 155
Loading
deleted 8 characters in body; edited tags
Source Link
Géry Ogam
  • 8.4k
  • 5
  • 53
  • 89
Loading
edited tags
Link
kmario23
  • 62.1k
  • 17
  • 174
  • 160
Loading
Edited question title and removed language tag.
Link
martineau
  • 124.1k
  • 29
  • 181
  • 319
Loading
edited title
Link
codeforester
  • 43.8k
  • 21
  • 122
  • 159
Loading
edited title
Link
cs95
  • 406.3k
  • 106
  • 744
  • 797
Loading
Edited question title and removed language tag.
Link
martineau
  • 124.1k
  • 29
  • 181
  • 319
Loading
Rollback to Revision 7
Link
Michał Perłakowski
  • 93.4k
  • 30
  • 165
  • 189
Loading
Edited question title and removed language tag.
Link
martineau
  • 124.1k
  • 29
  • 181
  • 319
Loading
deleted 23 characters in body
Source Link
Aaron Hall
  • 400.2k
  • 93
  • 416
  • 342
Loading
edited tags
Link
thefourtheye
  • 240.6k
  • 53
  • 466
  • 505
Loading
added 20 characters in body; edited tags; edited title
Source Link
Aaron Hall
  • 400.2k
  • 93
  • 416
  • 342
Loading
deleted 1 characters in body; edited title
Source Link
arshajii
  • 129.9k
  • 26
  • 246
  • 293
Loading
Question Protected by Jon Clements
deleted 66 characters in body; edited title
Source Link
Peter O.
  • 33.1k
  • 14
  • 86
  • 97
Loading
Source Link
Simon
  • 81.2k
  • 27
  • 93
  • 120
Loading