Slice Lists in Python

(Last Updated On: 25/01/2023)

Python Program to Display a Whole List Using Slicing

# Python Program to Display a Whole List Using Slicing

# Initialize list
list = [1,2,3,4,5,6,7,8,9]
 
# Display the whole list
print(list[::])

Output:

[1, 2, 3, 4, 5, 6, 7, 8, 9]

Python Program to Display a Whole List Before a Specific Position Using Slicing

# Python Program to Display a Whole List Before a Specific Position Using Slicing
 
# Initialize list
list = [1,2,3,4,5,6,7,8,9]
 
# Display Specific Position list
print(list[:2])

Output:

[1, 2]

Python Program to Display a Whole List From One Position to Another Position Using Slicing

# Display a Whole List From One Position to Another Position Using Slicing
 
# Initialize list
list = [1,2,3,4,5,6,7,8,9]
 
# Display Specific Position list
print(list[2:4])

Output:

[3, 4]

Leave a Reply

Your email address will not be published. Required fields are marked *