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[:])
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Python-Programs,
- Program 5: Find Sum of Two Integer Numbers - 1000+ Python Programs
- 34: Traverse a List in Reverse Order - 1000+ Python Programming
- 22: Send Yahoo! Email using smtplib - SMTP protocol client using Python Program
- 35: Python Program to find the System Hostname
- 27: Measure Elapsed Time for a Python Program Execution
- Program 7: Find Difference of Two Numbers - 1000+ Python Programs
- Program 12: Calculate Area and Circumference of Circle - 1000+ Python Programs
- Program 9: Divide Two Numbers - 1000+ Python Programs
- Program 2: Print your name using print() function - 1000+ Python Programs
- 25: How to rename a file using Python Program
- 17: Find Factorial of a Number - 1000+ Python Programs
- Program 6: Find Sum of Two Floating Numbers - 1000+ Python Programs
- 23: Python Programs to concatenate two Lists
- 36: Python Program Convert Hex String to Integer
- 20 - Python - Print Colors for Text in Terminal - 1000+ Python Programs
- Python Program: Use NumPy to generate a random number between 0 and 1
- 32: Python Program to Find Square Root of a Number
- Program 8: Multiply Two Numbers - 1000+ Python Programs
- Program 11: Calculate Percentage - 1000+ Python Programs
- 18: Get Sub List By Slicing a Python List - 1000+ Python Programs
- 28: Program to Lowercase a String in Python
- Program 1: Print Hello World! - 1000+ Python Programs
- 21: Program to Delete File or Folder in Python
- 29: Program to convert Python dict to dataframe
- 33: Python Program to find the current time in India (IST)
More Posts:
- Android Emulator] ##KBD: Full queue, lose event Error Logs - Android
- Install Jupyter Notebook without Anaconda on Mac - MacOS
- How to minimize all Windows in Mac - MacOS
- How to Change Mouse Wheel Scroll Direction on Mac - MacOS
- Notepad++ delete lines above or below - NotepadPlusPlus
- Online UUID Generator Tool - Tools
- Java: Fix SAXParseException: :1:1: Content is not allowed in prolog. - Java
- Java JDBC Example with Oracle Database Driver Connection - Java