Python List Slicing - GeeksforGeeks

10 Jun, 2021
In Python, list slicing is a common practice and it is the most used technique for programmers to solve efficient problems. Consider a python list, In-order to access a range of elements in a list, you need to slice a list. One way to do this is to use the simple slicing operator i.e. colon(:)
With this operator, one can specify where to start the slicing, where to end, and specify the step. List slicing returns a new list from the existing list.
Syntax:
Lst[ Initial : End : IndexJump ]
If Lst is a list, then the above expression returns the portion of the list from index Initial to index End, at a step size IndexJump.
1. Positive Indexes
Below is a simple program, to display a whole list using slicing.
Python3
# Initialize listLst = [50, 70, 30, 20, 90, 10, 50] # Display listprint(Lst[::])
Output:
[50, 70, 30, 20, 90, 10, 50]
The above diagram illustrates a list Lst with its index values and elements.
2. Negative Indexes
Now, let us look at the below diagram which illustrates a list along with its negative indexes.
Index -1 represents the last element and -n represents the first element of the list(considering n as the length of the list). Lists can also be manipulated using negative indexes also.
Python3
# Initialize listLst = [50, 70, 30, 20, 90, 10, 50] # Display listprint(Lst[-7::1])
Output:
[50, 70, 30, 20, 90, 10, 50]
The above program displays the whole list using the negative index in list slicing.
3. Slicing
As mentioned earlier list slicing is a common practice in Python and can be used both with positive indexes as well as negative indexes. The below diagram illustrates the technique of list slicing:
The below program transforms the above illustration into python code:
Python3
# Initialize listLst = [50, 70, 30, 20, 90, 10, 50] # Display listprint(Lst[1:5])
Output:
[70, 30, 20, 90]
Below are some examples which depict the use of list slicing in Python:
Example 1:
Python3
# Initialize listList = [1, 2, 3, 4, 5, 6, 7, 8, 9] # Show original listprint("\nOriginal List:\n", List) print("\nSliced Lists: ") # Display sliced listprint(List[3:9:2]) # Display sliced listprint(List[::2]) # Display sliced listprint(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 will lead to the use of default values i.e 0 as Initial, length of list as End and 1 as IndexJump.
Example 2:
Python3
# Initialize listList = ['Geeks', 4, 'geeks !'] # Show original listprint("\nOriginal List:\n", List) print("\nSliced Lists: ") # Display sliced listprint(List[::-1]) # Display sliced listprint(List[::-3]) # Display sliced listprint(List[:1:-2])
Output:
Original List:
 ['Geeks', 4, 'geeks !']

Sliced Lists: 
['geeks !', 4, 'Geeks']
['geeks !']
['geeks !']
A reversed list can be generated by using a negative integer as the IndexJump argument. Leaving the Initial and End as blank. We need to choose the Initial and End value according to a reversed list if the IndexJump value is negative. 
Example 3:
Python3
# Initialize listList = [-999, 'G4G', 1706256, '^_^', 3.1496] # Show original listprint("\nOriginal List:\n", List) print("\nSliced Lists: ") # Display sliced listprint(List[10::2]) # Display sliced listprint(List[1:1:1]) # Display sliced listprint(List[-1:-1:-1]) # Display sliced listprint(List[:0:])
Output:
Original List:
 [-999, 'G4G', 1706256, '^_^', 3.1496]

Sliced Lists: 
[]
[]
[]
[]
If some slicing expressions are made that do not make sense or are incomputable then empty lists are generated.
Example 4:
Python3
# Initialize listList = [-999, 'G4G', 1706256, 3.1496, '^_^'] # Show original listprint("\nOriginal List:\n", List)  print("\nSliced Lists: ") # Modified ListList[2:4] = ['Geeks', 'for', 'Geeks', '!'] # Display sliced listprint(List) # Modified ListList[:6] = [] # Display sliced listprint(List)
Output:
Original List:
 [-999, 'G4G', 1706256, 3.1496, '^_^']

Sliced Lists: 
[-999, 'G4G', 'Geeks', 'for', 'Geeks', '!', '^_^']
['^_^']
List slicing can be used to modify lists or even delete elements from a list.
Example 5:
Python3
# Initialize listList = [1, 2, 3, 4, 5, 6, 7, 8, 9] # Show original listprint("\nOriginal List:\n", List) print("\nSliced Lists: ") # Creating new ListnewList = List[:3]+List[7:] # Display sliced listprint(newList) # Changing existing ListList = List[::2]+List[1::2] # Display sliced listprint(List)
Output:
Original List:
 [1, 2, 3, 4, 5, 6, 7, 8, 9]

Sliced Lists: 
[1, 2, 3, 8, 9]
[1, 3, 5, 7, 9, 2, 4, 6, 8]
By concatenating sliced lists, a new list can be created or even a pre-existing list can be modified. 
sweetyty
sumitgumber28
python-list
Python
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Defaultdict in Python
Python | Get unique values from a list
Python | os.path.join() method
Selecting rows in pandas DataFrame based on conditions
Create a directory in Python
Python | Pandas dataframe.groupby()