In order to get a sublist by slicing of a Python List, you can use the slice notation which is a colon (:). It is used to separate the start and end indices of the sublist you want to extract.
Slicing Syntax:
list[start:end:step]
start | It is the index of the first element that you want to include in the sublist. |
end | It is a parameter that is used to specify the step size between each element (optional). |
step | It is the index of the first element that you want to exclude from the sublist. |
Finding the even numbers using slicing
python_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_sublist = python_list[1::2] # step = 2
print(even_sublist)
Some more examples with List Slicing
Let us consider the same list of numbers from 1 to 10 and answer the below questions.
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
- Get a sublist from index 2 to index 6.
print(my_list[2:6])
- Get a sublist from index 2 to index 6 with a step of 2.
print(python_list[2:6:2])
- Get a sublist from index 6 to the end of the list
print(python_list[6:])
- Get a copy of the original list
print(python_list[:])
Have Questions? Post them here!
- Program 11: Calculate Percentage - 1000+ Python Programs
- Program 8: Multiply Two Numbers - 1000+ Python Programs
- Program 1: Print Hello World! - 1000+ Python Programs
- 20 - Python - Print Colors for Text in Terminal - 1000+ Python Programs
- Program 15: Find String is a Palindrome or Not - 1000+ Python Programs
- 17: Find Factorial of a Number - 1000+ Python Programs
- Program 13: Reverse a String - 1000+ Python Programs
- 18: Get Sub List By Slicing a Python List - 1000+ Python Programs
- Program 9: Divide Two Numbers - 1000+ Python Programs
- Program 7: Find Difference of Two Numbers - 1000+ Python Programs
- Program 14: Sum of Even Numbers from 1 to 100 - 1000+ Python Programs
- Program 10: Modulo of Two Numbers - 1000+ Python Programs
- 16: Find the largest element in a List - 1000+ Python Programs
- Python Program: Use NumPy to generate a random number between 0 and 1
- Program 2: Print your name using print() function - 1000+ Python Programs
- 19: Simple Calculator using built-in functions - 1000+ Python Programs
- Program 6: Find Sum of Two Floating Numbers - 1000+ Python Programs
- Program 12: Calculate Area and Circumference of Circle - 1000+ Python Programs
- Program 5: Find Sum of Two Integer Numbers - 1000+ Python Programs
- Program 4: Input Name and Age and Print - 1000+ Python Programs
- Program 3: Print the name of user as input - 1000+ Python Programs
- Your Android SDK is out of date or is missing templates. Please ensure you are using SDK version 22 or later. - Android
- How to install Python 3.11 on Mac - Python
- How to Sync iPhone with iPad - iOS
- How to install Zoom Add-in to Outlook (Mac) - MacOS
- Auto Refresh Webpage after every x Second or Minute using Meta Tag? - Html
- SDK Manager: failed to install : Android Studio on Windows - Android-Studio
- Notepad++ sort by name with example - NotepadPlusPlus
- What Java version is used for Minecraft 1.18 - Java