>>> from operator import itemgetter
>>> a = range(100)
>>> itemgetter(5,13,25)(a)
(5, 13, 25)
Answer from John La Rooy on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-list-slicing
Python List Slicing - GeeksforGeeks
In Python, list slicing allows out-of-bound indexing without raising errors. If we specify indices beyond the list length then it will simply return the available items. Example: The slice a[7:15] starts at index 7 and attempts to reach index ...
Published ย  July 23, 2025
Discussions

Python List Slicing with Arbitrary Indices - Stack Overflow
Is there a better way to extract arbitrary indices from a list in python? ... Where a is the array I want to slice, and [5,13,25] are the elements that I want to get. More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to slice a list from an element n to the end in Python? - Stack Overflow
To my understanding, slice means lst[start:end], and including start, excluding end. So how would I go about finding the "rest" of a list starting from an element n? More on stackoverflow.com
๐ŸŒ stackoverflow.com
Slicing list of lists in Python - Stack Overflow
Why? he needs the first idx elements from each list. 2016-04-05T20:32:37.85Z+00:00 ... @WayneWerner correct but I changed the meaning of idx...instead of the slice it's a raw int 2016-04-05T20:33:12.493Z+00:00 More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How does assignment work with list slices? - Stack Overflow
Python docs says that slicing a list returns a new list. Now if a "new" list is being returned I've the following questions related to "Assignment to slices" a = [1, 2, 3] a[0:2... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
University of Pittsburgh
sites.pitt.edu โ€บ ~naraehan โ€บ python3 โ€บ mbb8.html
Python 3 Notes: List Slicing
Python 3 Notes [ HOME | LING 1330/2330 ] Tutorial 8: List Slicing << Previous Tutorial Next Tutorial >> On this page: slice indexing with [:], negative indexing, slice and negative indexing on strings. Video Tutorial Python 3 Changes print(x,y) instead of print x, y Python 2 vs.
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ slicing
List slicing in Python - Python Morsels
March 8, 2024 - In Python, slicing looks like indexing with colons (:). You can slice a list (or any sequence) to get the first few items, the last few items, or all items in reverse.
๐ŸŒ
W3Schools
w3schools.com โ€บ Python โ€บ python_strings_slicing.asp
Python - Slicing Strings
Remove List Duplicates Reverse a String Add Two Numbers ยท Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... You can return a range of characters by using the slice syntax.
๐ŸŒ
The Python Coding Stack
thepythoncodingstack.com โ€บ the python coding stack โ€บ a slicing story
A Slicing Story - by Stephen Gruppetta
June 25, 2024 - You can read more about __getitem__() in The Manor House, the Oak-Panelled Library, the Vending Machine, and Python's `__getitem__()`. In this new class called TestList, which inherits from list, you first print the value and data type of the argument in the __getitem__() method, and then you call the list's __getitem__() method and return its value. This is what super().__getitem__(item) does since list is the superclass for TestList. The syntax 2:7 within the square brackets represents a slice object.
Find elsewhere
๐ŸŒ
Kansas State University
textbooks.cs.ksu.edu โ€บ intro-python โ€บ 07-lists โ€บ 05-slicing-lists
Slicing Lists :: Introduction to Python
June 27, 2024 - A slice is simply a portion of a list that can be stored and used as a separate list, allowing us as programmers to quickly create and manipulate new lists based on existing lists. There are two basic ways to create a list slice in Python. nums[start:end] - this will create a slice of the list ...
๐ŸŒ
Prasad Ostwal
ostwalprasad.github.io โ€บ machine-learning โ€บ Python-List-Slicing-Cheatsheet.html
Python List Slicing Cheatsheet ยท
August 8, 2019 - list[-7::2] >> ['C', 'E', 'G', 'I'] list[-7::3] >> ['C', 'F', 'I'] list[1::-1] >> ['B', 'A'] list[1::-2] >> ['B'] list[1::-7] >> ['B'] list[6::-3] >> ['G', 'D', 'A'] If you have any more patterns, ideas or suggestions, please share. You can fork/start this at https://github.com/ostwalprasad/PythonListSlicingCheatsheet Want to build own site like this within 5 minutes?
Top answer
1 of 5
165

You are confusing two distinct operation that use very similar syntax:

1) slicing:

b = a[0:2]

This makes a copy of the slice of a and assigns it to b.

2) slice assignment:

a[0:2] = b

This replaces the slice of a with the contents of b.

Although the syntax is similar (I imagine by design!), these are two different operations.

2 of 5
101

When you specify a on the left side of the = operator, you are using Python's normal assignment, which changes the name a in the current context to point to the new value. This does not change the previous value to which a was pointing.

By specifying a[0:2] on the left side of the = operator, you are telling Python you want to use slice assignment. Slice assignment is a special syntax for lists, where you can insert, delete, or replace contents from a list:

Insertion:

>>> a = [1, 2, 3]
>>> a[0:0] = [-3, -2, -1, 0]
>>> a
[-3, -2, -1, 0, 1, 2, 3]

Deletion:

>>> a
[-3, -2, -1, 0, 1, 2, 3]
>>> a[2:4] = []
>>> a
[-3, -2, 1, 2, 3]

Replacement:

>>> a
[-3, -2, 1, 2, 3]
>>> a[:] = [1, 2, 3]
>>> a
[1, 2, 3]

Note:

The length of the slice may be different from the length of the assigned sequence, thus changing the length of the target sequence, if the target sequence allows it. - source

Slice assignment provides similar function to tuple unpacking. For example, a[0:1] = [4, 5] is equivalent to:

# Tuple Unpacking
a[0], a[1] = [4, 5]

With tuple unpacking, you can modify non-sequential lists:

>>> a
[4, 5, 3]
>>> a[-1], a[0] = [7, 3]
>>> a
[3, 5, 7]

However, tuple unpacking is limited to replacement, as you cannot insert or remove elements.

Before and after all these operations, a is the same exact list. Python simply provides nice syntactic sugar to modify a list in-place.

๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ slicing-and-indexing-in-python
Slicing and Indexing in Python โ€“ Explained with Examples
December 11, 2025 - In Python, you perform slicing using the colon : operator. The syntax for slicing is as follows: ... where start_index is the index of the first element in the sub-sequence and end_index is the index of the last element in the sub-sequence (excluding the element at the end_index). To slice a sequence, you can use square brackets [] with the start and end indices separated by a colon...
๐ŸŒ
Railsware
railsware.com โ€บ home โ€บ engineering โ€บ indexing and slicing for lists, tuples, strings, other sequential types in python
Python Indexing and Slicing for Lists, Tuples, Strings, other Sequential Types | Railsware Blog
January 22, 2025 - In short, slicing is a flexible tool to build new lists out of an existing list. Python supports slice notation for any sequential data type like lists, strings, tuples, bytes, bytearrays, and ranges.
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ list slicing in python
List Slicing in Python
November 11, 2024 - Python List Slicing is a technique for extracting specific portions of a list. The syntax for list slicing is: Lst: The list you want to slice.
๐ŸŒ
Indiachinainstitute
indiachinainstitute.org โ€บ wp-content โ€บ uploads โ€บ 2018 โ€บ 05 โ€บ Python-Crash-Course.pdf pdf
Python Crash Course
Copying a List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 ยท Exercise 4-10: Slices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 3340497 โ€บ what-is-slicing-of-data-in-lists-
What is slicing of data in lists
Sololearn is the world's largest community of people learning to code. With over 25 programming courses, choose from thousands of topics to learn how to code, brush up your programming knowledge, upskill your technical ability, or stay informed about the latest trends.
๐ŸŒ
Canard Analytics
canardanalytics.com โ€บ blog โ€บ python-list-slicing
A Guide to Python List Slicing | Canard Analytics
June 8, 2022 - # list slice notation sub_list = original_list[start:stop:step] # list slice object sub_list = original_list[slice(start,stop,step)] # The two notations are equivalent
๐ŸŒ
Itsourcecode
itsourcecode.com โ€บ home โ€บ what is list slicing python? how it works with examples
What is List Slicing Python? How it Works With Examples - Itsourcecode.com
May 13, 2026 - Slicing of lists in Python refers to accessing a specific part or subset of the list for a particular operation while the original list stays unchanged. The slicing operator in Python can work with three parameters, but two of them can be left ...
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ datastructures.html
5. Data Structures โ€” Python 3.14.6 documentation
Return zero-based index of the first occurrence of value in the list. Raises a ValueError if there is no such item. The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list.