>>> 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
🌐
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....
🌐
Python Morsels
pythonmorsels.com › slicing
List slicing in Python - 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....
🌐
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.
🌐
Learn By Example
learnbyexample.org › python-list-slicing
Python List Slicing - Learn By Example
June 20, 2024 - In this example, we start at index ... 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 ...
🌐
Real Python
realpython.com › lessons › indexing-and-slicing
Indexing and Slicing (Video) – Real Python
That could be confusing because if you’ve worked with strings before, if you have a string, s, which is 'mybacon', using the syntax of just the [:] does return the entire string. 05:20 What’s interesting about it is not only is s == s with the colon-only index, but it also is a reference to that object. Whereas with a list, it returns an entirely new object. 05:37 It’s possible to add a third index after the additional colon. That third index indicates a stride, but it’s also sometimes called a step. So in this example, if you had a slice that went from 0 to 6, with a step of 2, you would return the objects at index 0, 2, and 4—'spam', 'bacon', and 'ham'.
Published   September 3, 2019
🌐
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 ...
Find elsewhere
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › python list slice with examples
Python List Slice with Examples - Spark By {Examples}
May 31, 2024 - For example, lists[1:4] specify a range of indices to modify, starting from the index 1 and ending at the index 3 (inclusive). The assignment statement then sets the values of those items to the list ['C++', 'Hyperion', 'NumPy']. # Slice list ...
🌐
TutorialsPoint
tutorialspoint.com › How-to-index-and-slice-lists-in-Python
How to index and slice lists in Python?
The following is an example code to show the negative indexing of lists. list= [5,2,9,7,5,8,1,4,3] print(list[-2]) print(list[-8]) When you run the program, it will show this output- ... List slicing is a frequent practice in Python, and it is the most prevalent technique used by programmers to solve problems. Consider a Python list. You must slice a list in order to access a range of elements in it.
🌐
UC Berkeley Statistics
stat.berkeley.edu › ~spector › extension › python › notes › node29.html
List Indexing and Slicing
>>> # using a single subscript >>> x = ['one','two','three','four','five'] >>> x[1] = ['dos','tres','cuatro'] >>> x ['one', ['dos', 'tres', 'cuatro'], 'three', 'four', 'five'] >>> # using a slice >>> x = ['one','two','three','four','five'] >>> x[1:1] = ['dos','tres','cuatro'] >>> x >>> ['one', 'dos', 'tres', 'cuatro', 'two', 'three', 'four', 'five'] In the final example, we were able to insert three elements into an list without replacing any elements in the list by assigning to a slice where both subscripts were the same. Another use of slices is to make a separate modifiable copy of a list. (See Section 6.1 to understand why this is important.) In this case, you create a slice without either a starting or ending index. Python will then make a complete copy of the list
🌐
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 :.
🌐
Gitbooks
buzzcoder.gitbooks.io › codecraft-python › content › list › list-indexing-slicing.html
List indexing and slicing · CodeCraft-Python
# list slice numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] print(numbers[8:]) # [9, 10, 11, 12] print(numbers[:4]) # [1, 2, 3, 4] print(numbers[1:6]) # [2, 3, 4, 5, 6] print(numbers [::3]) # [1, 4, 7, 10] (every third number) ... # reverse list num = [0,1,2,3,4,5,6] numR = num[::-1] # reverse ...
🌐
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.
🌐
Python Tutorial
pythontutorial.net › home › python basics › python list slice
Python List Slice
March 26, 2025 - colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'] sub_colors = colors[1:4] print(sub_colors) Code language: Python (python) ... The begin index is 1, so the slice starts from the 'orange' color. The end index is 4, therefore, the last element of the slice is 'green'.
🌐
Medium
medium.com › @krejjas › a-surprising-property-of-python-list-slicing-with-indices-84da3be90643
A surprising property of Python list slicing with indices | by Jasenko Krejić | Medium
September 25, 2025 - To handle this, we can use a try…except block if we expect such an error to occur, or we can first check whether the index is valid by using the len() function to ensure it falls within the allowed range. You’ve also probably used list slicing — a way to extract a sublist from an existing list. Slicing returns elements between the start index (inclusive) and the end index (exclusive). These indices are separated by a colon (:).
🌐
Medium
medium.com › @kalavensut03 › all-about-slicing-in-python-b6f31f88cefb
All about Slicing in Python. When working with lists (or any… | by Suryatejakalapala | Medium
August 22, 2024 - Off-by-One Errors: Remember that the end index is not included in the slice. It’s easy to forget and accidentally exclude the last element you wanted. Empty Lists: If your start index is greater than or equal to your end index, Python will return an empty list, which can be unexpected. Out-of-Range Indices: Python won’t throw an error if you slice with an index that’s out of range.