Algorithm


  1. Input:

    • Take input for the list.
    • Take input for the start and end indices for slicing.
  2. Slice Operation:

    • Use the list slicing syntax list[start:end] to extract a portion of the list.
    • The start index is inclusive, and the end index is exclusive.
  3. Output:

    • Display or return the sliced portion of the list.

 

Code Examples

#1 Code Example- Get all the Items

Code - Python Programming

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

print(my_list[:])
Copy The Code & Try With Live Editor

Output

x
+
cmd
[1, 2, 3, 4, 5]

#2 Code Example- Get all the Items After a Specific Position

Code - Python Programming

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

print(my_list[2:])
Copy The Code & Try With Live Editor

Output

x
+
cmd
[3, 4, 5]

#3 Code Example- Get all the Items Before a Specific Position

Code - Python Programming

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

print(my_list[:2])
Copy The Code & Try With Live Editor

Output

x
+
cmd
[1, 2]

#4 Code Example- Get all the Items from One Position to Another Position

Code - Python Programming

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

print(my_list[2:4])
Copy The Code & Try With Live Editor

Output

x
+
cmd
[3, 4]

#5 Code Example- Get the Items at Specified Intervals

Code - Python Programming

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

print(my_list[::2])
Copy The Code & Try With Live Editor

Output

x
+
cmd
[1, 3, 5]

#6 Code Example with Python Programming

Code - Python Programming

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

print(my_list[::-2])
Copy The Code & Try With Live Editor

Output

x
+
cmd
[5, 3, 1]

#7 Code Example with Python Programming

Code - Python Programming

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

print(my_list[1:4:2])
Copy The Code & Try With Live Editor

Output

x
+
cmd
[2, 4]
Advertisements

Demonstration