Write a Python Program to Slice Lists

Python Program to Slice Lists

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn  Python programming concepts. You can learn Python language at your own speed and time. One can learn concepts of Python language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Python Program to Slice Lists
Python Program to Slice Lists

Write a Python Program to Slice Lists

PROGRAM: Python Program to Slice Lists

/* Python Program to Slice Lists */

Get all the Items

my_list = [1, 2, 3, 4, 5]

print(my_list[:])

Output

[1, 2, 3, 4, 5]

Get all the Items After a Specific Position

my_list = [1, 2, 3, 4, 5]

print(my_list[2:])

Output

[3, 4, 5]

Get all the Items Before a Specific Position

my_list = [1, 2, 3, 4, 5]

print(my_list[:2])

Output

[1, 2]

Get all the Items from One Position to Another Position

my_list = [1, 2, 3, 4, 5]

print(my_list[2:4])

Output

[3, 4]

Get the Items at Specified Intervals

my_list = [1, 2, 3, 4, 5]

print(my_list[::2])

Output

[1, 3, 5]
 
 
 
 

Leave a Reply

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