Python list slicing
In this post, you will learn about the list slicing using the Python programming language.
Python List
A list is an arranged collection of elements. It is used to store collections of data. It can contain a list of various types of data objects with a comma separated and enclosed within a square bracket. It is mutable, which means we can change the order of elements, and individual elements can be replaced even after the list has been created. This is one of the leading reasons why lists are being used widely. Lists play an effective role in small projects as they consume more memory.
Python List Slicing
Syntax-[start:stop:step]
Here, start is the index of the list where slicing starts, stop is the index of the list where slicing ends, and step allows us to select the nth item within the range start to stop. Here, we have mentioned different list slicing Python programs.
Python display all list elements
# Python display all list elements
planet_list = ['Mercury','Venus','Earth','Mars','Jupiter','Saturn','Uranus','Neptune']
# displaying planet names
print(planet_list[:])
Output of the above code:
['Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn', 'Uranus', 'Neptune']
Python list slicing after a specific position
To get all the elements after a specific index, mention that index before : as shown in the given example.
# Python display all list elements
planet_list = ['Mercury','Venus','Earth','Mars','Jupiter','Saturn','Uranus','Neptune']
# displaying planet names
print(planet_list[4:])
Output of the above code:
['Jupiter', 'Saturn', 'Uranus', 'Neptune']
Python list slicing before a specific position
To get all the elements before a specific index, mention that index after : as shown in the given example.
# Python display all list elements
planet_list = ['Mercury','Venus','Earth','Mars','Jupiter','Saturn','Uranus','Neptune']
# displaying planet names
print(planet_list[:4])
Output of the above code:
['Mercury', 'Venus', 'Earth', 'Mars']
Python list slicing in given position range
# Python display all list elements
planet_list = ['Mercury','Venus','Earth','Mars','Jupiter','Saturn','Uranus','Neptune']
# displaying planet names
print(planet_list[2:6])
Output of the above code:
['Earth', 'Mars', 'Jupiter', 'Saturn']
Python list slicing at specified intervals
To get elements at specified intervals, use the index two: as shown in the example.
# Python display all list elements
planet_list = ['Mercury','Venus','Earth','Mars','Jupiter','Saturn','Uranus','Neptune']
# displaying planet names
print(planet_list[[::2]])
Output of the above code:
['Mercury', 'Earth', 'Jupiter', 'Uranus']
Python List slicing with negative indices
# Python display all list elements
planet_list = ['Mercury','Venus','Earth','Mars','Jupiter','Saturn','Uranus','Neptune']
# displaying planet names
print(planet_list[-4:-2])
Output of the above code:
['Jupiter', 'Saturn']
Python list slicing with positive & negative indices
# Python display all list elements
planet_list = ['Mercury','Venus','Earth','Mars','Jupiter','Saturn','Uranus','Neptune']
# displaying planet names
print(planet_list[2:-3])
Output of the above code:
['Earth', 'Mars', 'Jupiter']
Related Articles
Count consonants in a string Python
Python split strings by comma
Count vowels in a string Python
Replace multiple characters Python
Convert array to list Python
Python loop through list
Difference between tuple and list in Python
Convert string to list Python
Remove last element from list Python
Convert list to string Python
Convert Python list to numpy array
Remove element from list Python
Python dict inside list
Check if list is empty Python
Python iterate list with index
Python iterate list with index
Python split string by comma
Printing Simple Diamond Pattern in Python
Stemming and Lemmatization in Python
Python | Generate QR Code using pyqrcode module