Learn Python
- Python basic
- Introduction to File Handling
- Basics of List Comprehension
- Introduction to Matplotlib
- Classes and Objects
- Introduction to Functions
- Python Numbers
- Creating Basic Plots
- Opening and closing files
- Function parameters and arguments
- Advanced Techniques
- Attributes and Methods
- Python Strings
- Scope and lifetime of variables
- Advanced Plotting
- Reading from files
- Performance and Limitations
- Encapsulation
- Python List
- Specialized Plots
- Writing to files
- Return statement and output
- Inheritance
- Python Tuple
- Advanced Customization
- Working with different file formats
- Lambda Functions
- Polymorphism
- Practical Applications
- Special Methods
Python List
List slicing
List slicing is a powerful technique in Python that allows you to extract a sublist (a portion of the list) based on specific starting and ending positions. It uses a colon (:) syntax with optional start and stop indices within square brackets [].
1. Basic slicing
my_list = ["apple", "banana", "cherry", "orange", "mango"] sublist = my_list[1:3] # This extracts elements at index 1 (inclusive) to 3 (exclusive) print(sublist) # Output: ["banana", "cherry"]
In this example, the sublist captures elements from index 1 ("banana") to index 2 ("cherry") but excludes the element at index 3 ("orange").
2. Specifying start and stop indices
You can explicitly define the starting and ending positions for the slice:
You can explicitly define the starting and ending positions for the slice:
my_list = ["apple", "banana", "cherry", "orange", "mango"] first_two = my_list[0:2] # Get elements from index 0 (inclusive) to 2 (exclusive) last_three = my_list[2:] # Get elements from index 2 (inclusive) to the end print(first_two) # Output: ["apple", "banana"] print(last_three) # Output: ["cherry", "orange", "mango"]
Here, first_two grabs elements from the beginning (index 0) up to but not including index 2. last_three fetches elements from index 2 (inclusive) to the end of the list.
3. Omitting start or stop indices
Leaving the start or stop index empty assumes default values:
- An empty start index defaults to the beginning (index 0).
- An empty stop index defaults to the end of the list.
my_list = ["apple", "banana", "cherry", "orange", "mango"] all_but_last = my_list[:-1] # Get all elements except the last one first_three = my_list[:3] # Get elements from the beginning up to index 3 (exclusive) print(all_but_last) # Output: ["apple", "banana", "cherry", "orange"] print(first_three) # Output: ["apple", "banana", "cherry"]
4. Negative indices
Similar to accessing elements, you can use negative indices for start and stop positions to count from the end of the list.
my_list = ["apple", "banana", "cherry", "orange", "mango"] last_two = my_list[-2:] # Get the last two elements all_but_first = my_list[1:] # Get all elements except the first one (similar to using an empty start index) print(last_two) # Output: ["orange", "mango"] print(all_but_first) # Output: ["banana", "cherry", "orange", "mango"]
It's time to take a quiz!
Test your knowledge and see what you've just learned.
What does the expression my_list[1:3] return?
A["banana", "cherry"]
B["apple", "banana", "cherry"]
C["cherry", "orange"]
D["orange", "mango"]
Check Answer
What does my_list[0:2] return?
A["apple", "banana"]
B["banana", "cherry"]
C["cherry", "orange"]
D["orange", "mango"]
Check Answer
What does my_list[:-1] return?
AAll elements including the last one.
BAll elements except the last one.
COnly the last element.
DAn empty list.
Check Answer
What does my_list[-2:] return?
AThe last element.
BThe first two elements.
CThe last two elements.
DAll elements except the last one.
Check Answer
What does my_list[1:] return?
DAll elements from the start.
AAll elements including the first one.
BAll elements except the first one.
CAll elements except the last one.
Check Answer