How do you slice a list within a list python?

Slicing list in Python

With the help of slicing, we can get the required elements from the list (sublist).

Slicing won't affect the original list. It will return the new list with the required elements.

Example

list[start index : end index]

starting index value will be included in the list and the ending index value will be excluded from the list.


Example

#Slice the lists in python

numbers =       [10, 20, 30, 40, 50]
#forward index   0    1   2   3   4
#reverse index  -5   -4  -3  -2  -1

print(numbers[0:3])   #from index 0 to 2. [10, 20, 30] (index 3 i.e 40 excluded)
print(numbers[-4:-1]) #from index -4 to -2. [20, 30, 40] (index -1 i.e 50 excluded)


numbers[0:3] will return the new sublist [10, 20, 30]. Index 3 i.e. value 40 will be excluded.

numbers[-4:-1] will return the new sublist [20, 30, 40]. Index -1 i.e value 50 will be excluded.




Slicing with default values

We can also use the default value in slicing by omitting either start index or end index.

The omitted first index will be set to 0.

The omitted end index will be set to length of the list (not end index of the list).


numbers = [10, 20, 30, 40, 50]

numbers[:index] ===> numbers[0:index]
numbers[index:] ===> numbers[index:5] #length of the list is 5.

Example

#Slice the lists in python

numbers =       [10, 20, 30, 40, 50]
#forward index   0    1   2   3   4
#reverse index  -5   -4  -3  -2  -1

#from index 0 to 1. [10, 20]    (index 2 i.e 30 excluded)
print("Index 0 to 1 = ", numbers[:2])

#from index 2 to the end. [30, 40, 50] (index 2 i.e 30 included )
print("Index 2 to 4 = ", numbers[2:])      

#which makes sure that numbers[:index] + numbers[index:] is always equal to numbers
print("List elements are ", numbers[:2]+numbers[2:])

#from index -5 to -4. [10, 20] (index -3 i.e 30 excluded)
print("Index -5 to -4 = ", numbers[:-3])   
#from index -3 to -1. [30, 40, 50] (index -3 i.e. 30 included)
print("Index -3 to -1 = ", numbers[-3:])  
print("List elements are ", numbers[:-3]+numbers[-3:])





Copy of the List

We can get the copy of the whole list by using list[:].

This is very useful when we want to manipulate the list but don't want to change the actual contents of it.


Example

#new copy of the list

numbers = [1, 2, 3]
numbers_copy = numbers[:]  #numbers[:] will return the new copy of the list

print("Copy of the list ", numbers_copy)





Assignment to Slices

Assigning values to the sliced sublist.


Example

#Assignment to slices

numbers = [1, 2, 3, 4, 5]
numbers[0:3] = [-1, -2, -3] #changing values from index 0 to 2 {0, 1, 2}

print(numbers)





Removing Elements from the List

We can remove any sublist from the list by replacing all the sublist elements with an empty list.


Example

#Removing elements from the list

numbers = [10, 20, 30, 40, 50]
numbers[1:3] = []  #Removing elements from index 1 to 2. i.e {20,30}

print(numbers)





Clear the Whole List

We can also clear the whole list by replacing all the elements with an empty list.


Example

#Clear the whole list

numbers = [1, 2, 3, 4, 5]
numbers[:] = []

print(numbers)



Topics You Might Like

Can you slice a list of lists in Python?

List slicing can be used to modify lists or even delete elements from a list.

How do you slice a list in Python?

How to slice a list in Python.
Define the list you want to slice. Retrieve the list you're slicing into sublists or extended cells. ... .
Enter the "start" and "stop" indexes. ... .
Add a positive "step" value. ... .
Add a negative "step" value..

How do you split a list into multiple lists in Python?

Split Lists into Chunks Using NumPy.
We turn our list into a Numpy array..
We split our array into n number of arrays using the np. array_split() function..
Finally, we use a list comprehension to turn all the arrays in our list of arrays back into lists..

Can we do slicing in list?

As well as using slicing to extract part of a list (i.e. a slice on the right hand sign of an equal sign), you can set the value of elements in a list by using a slice on the left hand side of an equal sign. In python terminology, this is because lists are mutable objects, while strings are immutable.