>>> 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
List slicing is a technique used to extract a portion of a list by specifying a range of indices. It returns a new list containing the selected elements while leaving the original list unchanged.
Published: July 16, 2026
Discussions

How can I get the list slice using a list of indexes in Python? - Stack Overflow
In Perl I can easily select multiple array elements using a list of indexes, e.g. my @array = 1..11; my @indexes = (0,3,10); print "@array[ @indexes ]"; # 1 4 11 What's the canonical way to do thi... More on stackoverflow.com
๐ŸŒ stackoverflow.com
I am confused about Slicing a List.
No. players[0:3] means "start at index 0, go until index 3-1", so that's the first 3 elements of your list. If you want the last 3 you need to use negative indexes. print(players[-3:]) More on reddit.com
๐ŸŒ r/learnpython
17
1
February 11, 2025
arrays - Elegant slicing in python list based on index - Stack Overflow
Lists can only be indexed with a scalar or slice. So some sort of iteration is required. It's numpy arrays that can index with a list. ... First, note that indexing in Python begins at 0. So the indices you need will be [0, 2]. More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - Slice an array with a list of indices - Stack Overflow
A problem not listed above. Try to be as specific as possible. ... You'll need to complete a few actions and gain 15 reputation points before being able to upvote. Upvoting indicates when questions and answers are useful. What's reputation and how do I get it? Instead, you can save this post to reference later. ... By ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
Learn By Example
learnbyexample.org โ€บ python-list-slicing
Python List Slicing - Learn By Example
June 20, 2024 - In this example, we start at index 2, which corresponds to the letter โ€˜cโ€™. Then we slice up to, but not including, index 7. This means we stop just before the letter โ€˜hโ€™ at index 7. The result is a new list that contains the elements at indices 2 through 6, giving us the desired sequence of letters from โ€˜cโ€™ to โ€˜gโ€™. When slicing lists in Python, you can use negative indices as well.
๐ŸŒ
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 this article, we will focus on indexing and slicing operations over Pythonโ€™s lists. Most of the examples we will discuss can be used for any sequential data type. Only mutable assignment and deletion operations are not applicable to immutable sequence types like tuples, strings, bytes, and ranges. Before discussing slice notation, we need to have a good grasp of indexing for sequential types.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ how to get a list slice with arbitrary indices in python?
How to Get a List Slice with Arbitrary Indices in Python? - Be on the Right Side of Change
August 27, 2024 - To extract elements with specific indices from a Python list, use slicing list[start:stop:step]. If you cannot use slicing because thereโ€™s no pattern in the indices you want to access, use the list comprehension statement [lst[i] for i in ...
๐ŸŒ
Real Python
realpython.com โ€บ lessons โ€บ indexing-and-slicing
Indexing and Slicing (Video) โ€“ Real Python
So in this example, if a is your list, then inside your square brackets you would have your two index numbers separated by a colon (:), and itโ€™s going to return a portion of that a list that will start with position m and go up to but not include index n. 02:38 Using that same example from before, if you took a[2:5], youโ€™d get the three objects in the middle, starting at index 2 and going up to index 4, but not including 5. 02:50 Let me have you try it out. Okay, so hereโ€™s your list. What if you wanted to slice?
Published: September 3, 2019
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ slicing-and-indexing-in-python
Slicing and Indexing in Python โ€“ Explained with Examples
December 11, 2025 - The syntax for slicing is as follows: ... element at the end_index). To slice a sequence, you can use square brackets [] with the start and end indices separated by a colon....
Find elsewhere
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ examples โ€บ list-slicing
Python Program to Slice Lists
In the example, the items before index 2 are sliced. Item on index 2 is excluded. ... If you want to get all the elements between two specific indices, you can mention them before and after :. In the above example, my_list[2:4] gives the elements between 2nd and the 4th positions. The starting position (i.e. 2) is included and the ending position (i.e. 4) is excluded. ... If you want to get elements at specified intervals, you can do it by using two :.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ How-to-index-and-slice-lists-in-Python
How to index and slice lists in Python?
The above expression returns the portion of the list from index Start to index Stop (exclusive), at a step size of Step. In the following example, we have used the slice operation to slice a list.
๐ŸŒ
Kansas State University
textbooks.cs.ksu.edu โ€บ intro-python โ€บ 07-lists โ€บ 05-slicing-lists
Slicing Lists :: Introduction to Python
July 30, 2026 - The method for creating list slices is very similar to how the range() function is used in Python. In effect, if the same values are provided as arguments to the range() function, then it will produce the list of indexes that will be used to generate the list slice.
๐ŸŒ
Python Morsels
pythonmorsels.com โ€บ slicing
Python list slicing (with examples) - Python Morsels
March 8, 2024 - What do you think we might get ... 'pear', 'lemon', 'orange'] With Python's slicing syntax, the first item is the start index, and the second item is the stop index....
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 58660455 โ€บ slice-an-array-with-a-list-of-indices
python - Slice an array with a list of indices - Stack Overflow
For example, this function would return dummy_function(2) = 18, and dummy_function(1) = 16 but I would like to evaluate it using a list as its argument: ... Instead I get IndexError: invalid slice. I don't want to use a loop to iterate over the elements of [2,1] because I believe it can be done more effectively.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ python-how-to-slice-list-with-variable-indexes-418690
How to slice list with variable indexes | LabEx
def safe_slice_extraction(data, start, end): try: return data[start:end] except IndexError: print("Invalid slice range") return [] sample_data = [10, 20, 30, 40, 50] result = safe_slice_extraction(sample_data, 2, 10) LabEx recommends practicing these practical slicing techniques to enhance your Python data manipulation skills. By mastering variable index slicing techniques in Python, developers can create more flexible and dynamic list manipulation strategies.
๐ŸŒ
Unstop
unstop.com โ€บ home โ€บ blog โ€บ python list slice | syntax, parameters & uses (+code examples)
Python List Slice | Syntax, Parameters & Uses (+Code Examples)
December 30, 2024 - List slicing in Python allows you to extract a range of elements between two indices by specifying both a start and a stop value. This is useful when you need a specific subset of the list, excluding everything before the starting index and ...