In this tutorial you will learn about the Python Program to Slice Lists and its application with practical example.
In this tutorial, we will learn to create a Python Program to Slice Lists using python Programming.
- Python slice() function.
- Python Program to Slice Lists.
- List Declaration.
- Items between One Position(1) to Another Position(5).
- Items between One Position to Another Position with index.
- Items between One Position to Another Position without index.
- Items till to Another Position.
- Same work can be done through slice() Function.
Before starting with this tutorial we assume that you are best aware of the following Python programming topics:
- Python Operator.
- Basic Input and Output
- Basic Python programming.
- Python Data type.
- List in Python.
Python slice() function.
The slice() method provided by Python that returns a portion of an List .With this Method(), we can specify the starting point of slicing and end point of slicing, and also the step. Output returns a List slice form original list to a new list from the existing one.
Python Program to Slice Lists.
In this program we will Slice a list like [start:stop:step].where Start is starting point Stop is ending point ans Step is index jump.Firstly we declare required header file and variable and also initiates required values in variable. Next we take value from user at run time and then after we will Slice a list to given points.
Here we will Demonstrate is a simple program, to display a whole list.
The above image shows a “List” Declaration and displaying all the elements of a list.
Items between One Position(1) to Another Position(5).
Let say from one two five items will be displayed.
Items between One Position to Another Position with index.
where 1 is start point 7 is end point and 2 is index value of list(Jumping index).
Items between One Position to Another Position without index.
If you want to take elements between two specific indices, you can mention them before and after [ : ].No need to assign index.
Items till to Another Position.
In that case you need to give only end point position elements form start to that point will be diplayed.
Same work can be done through slice() Function.
It will take three arguments Slice(Start:Stop:step)
- Start – Start where the slicing of the List starts.
- Stop -Slicing stops at i stop.
- Index (optional) – It will determines the increment index for slicing.