List Operations

Slicing List

In Python, a programming language, slicing list refers to a common technique for programmers in order to solve efficient problems. Moreover, the performance of a slice operation takes place on lists with the use of a colon(:). Furthermore, various ways exist for the purpose of printing the whole list with all the elements, but the use of slice operation takes place to print a specific range of elements from the list.

slicing list

                                                                                                                                          Slicing List

Demonstration of Slicing List

Below is a demonstration of slicing list:

                    
# Python program to demonstrate

# Removal of elements in a List



# Creating a List

List = ['T','O','P','P','R','F',

'O','R','T','O','P','P','R']

print("Intial List: ")

print(List)



# Print elements of a range

# using Slice operation

Sliced_List = List[3:8]

print("\nSlicing elements in a range 3-8: ")

print(Sliced_List)



# Print elements from a

# pre-defined point to end

Sliced_List = List[5:]

print("\nElements sliced from 5th "

"element till the end: ")

print(Sliced_List)



# Printing elements from

# beginning till end

Sliced_List = List[:]

print("\nPrinting all elements using slice operation: ")

print(Sliced_List)

                
Output:
                    

Intial List:

['T', 'O', 'P', 'P', 'R', 'F', 'O', 'R', 'T', 'O', 'P', 'P', 'R']



Slicing elements in a range 3-8:

['P', 'R', 'F', 'O', 'R']



Elements sliced from 5th element till the end:

['F', 'O', 'R', 'T', 'O', 'P', 'P', 'R']



Printing all elements using slice operation:

['T', 'O', 'P', 'P', 'R', 'F', 'O', 'R', 'T', 'O', 'P', 'P', 'R']

Browse more Topics Under List Operations

Negative Index List Slicing

Below is a demonstration of negative index list slicing:

                    

# Creating a List

List = ['T','O','P','P','R','F',

'O','R','T','O','P','P','R']

print("Initial List: ")

print(List)



# Print elements from beginning

# to a pre-defined point using Slice

Sliced_List = List[:-6]

print("\nElements sliced till 6th element from last: ")

print(Sliced_List)



# Print elements of a range

# using negative index List slicing

Sliced_List = List[-6:-1]

print("\nElements sliced from index -6 to -1")

print(Sliced_List)



# Printing elements in reverse

# using Slice operation

Sliced_List = List[::-1]

print("\nPrinting List in reverse: ")

print(Sliced_List)

Output

                    

Initial List:

['T', 'O', 'P', 'P', 'R', 'F', 'O', 'R', 'T', 'O', 'P', 'P', 'R']



Elements sliced till 6th element from last:

['T', 'O', 'P', 'P', 'R', 'F', 'O']



Elements sliced from index -6 to -1

['R', 'T', 'O', 'P', 'P']



Printing List in reverse:

['R', 'P', 'P', 'O', 'T', 'R', 'O', 'F', 'R', 'P', 'P', 'O', 'T']

Slicing List With IndexJump

Slicing list is certainly a popular practice in Python whose use can take place with both positive indexes and negative indexes. Below is a description to explain IndexJump.

                    

# Initialize list

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

# Show original list

print("\nOriginal List:\n", List)

print("\nSliced Lists: ")

# Display sliced list

print(List[3:9:2])

# Display sliced list

print(List[::2])

# Display sliced list

print(List[::])

Output

                    

Original List:

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

Sliced Lists:

[4, 6, 8]

[1, 3, 5, 7, 9]

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

Leaving any argument like Initial, End or IndexJump blank would result in the using of use of default values i.e 0 as Initial, length of list as End and 1 as IndexJump.

FAQs For Slicing List

Question 1: What is meant by slicing in Python?

Answer 1: Slicing list in python refers to taking elements from a particular index to another index. Furthermore, one has to pass slice instead of the index like this: [start:end]. Moreover, it is also possible to define the step like this: [start:end:step].

In case the start is not passed, then it is considered 0. Moreover, if there is no passing of end, then it would be considered the length of the array in that dimension.

Question 2: Does slicing means the creation of a new list?

Answer 2: Slicing list in Python is a flexible tool that results in the building of new lists out of an existing list. Moreover, Python supports slice notation for any sequential data types like ranges, bytes, bytearrays, tuples, lists, and strings. Also, addition can take place by any new data structure.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Browse

List Operations

Leave a Reply

avatar
  Subscribe  
Notify of

Browse

List Operations

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

Which class are you in?
No thanks.