Introduction to Python List
The list is one of the most used in Python for traversing the elements. It is used along with tuples, dictionaries, and sets. The list is a collection of items/data which is ordered and can be changed whenever needed. It also allows duplicate records in the data set. Firstly, we need to understand the functionality of the list and its characteristics. Using a list we can do multiple operations such as appending, slicing, adding and removing elements, indexing, etc. and there are many different functions in the list and it goes on. Below are some of the functionalities of lists in Python.
Types of Python Lists with Example
In Python, the list data structure is used when the sequence of items has to be in the ordered form or need to change for some operation. It is easily modified and also allows duplicate sets of records in many cases. It is declared with open and close square brackets.
Let’s understand each and every type of them briefly.
1. A Simple List
The list is always enclosed in square brackets and the elements inside it. It can be printed using the below-given example.
list new = ["papaya", "watermelon", "guava"]
In the above code, the list new is the name of the list and listnew means that the second element in the list has to be printed in the console output.
2. Accessing Items/Elements
This type will allow you to access the data elements/items in the list referring to the corresponding index number of the item.
ABC = ["guava", "papaya", "banana"]
In the above code, ABC is the name of the list and the element that is printed in the console is in the first position of the list. ABC  will print the first element of the list.
4.8 (4,289 ratings)
3. Negative Indexing in List
This type will print the elements in the reverse order i.e. it will start traversing the list in reverse mode instead of straightway. In this, the list index will have negative integer i.e. -1,-2, etc.
listabc = ["papaya", "watermelon", "guava"]
In the above code, the listabc is the name of the list and the output printed in the console is the last element of the list. listabc[-1] will print the last element of the list. listabc [-2] will print the second last element of the list and it goes on. When the user wants to reverse a list, this type is applicable in that case.
4. Index Range
This type is one of the user types in a list as we can print the data that is specified in between the indexes. This will print the elements of the list based on the specified index.
list new = ["papaya", "banana", "guava", "watermelon", "litchi", "apple", "cherry"]
In the above code, the element that is printed in the console output is in the range between the mentioned indexes. listnew[2:5] will print the elements that are present in the index range from 2 to index range 5.
5. Beginning Range
This type will print the elements present in the list till the mentioned index i.e. till the index number of the element.
list fruits = ["apple", "banana", "cherry", "orange", "kiwi”, “lemon"]
In the above code, the elements printed in the output console are the elements present before the mentioned index. In this example, list fruits are the name of the list and list fruits [:4] will print the elements that are present from the 1st to 4th index.
6. Ending Range
This type will print the elements present in the list from the mentioned index i.e. from the mentioned number to the end of the list.
fruits = ["apple", "melon", "cherry", "custard-apple", "kiwi", "dragonfruit"]
In the above code, fruits are the name of the list and the elements printed in the output console are the elements present after the specified index in the list. fruits [3:] will print the elements from the 3rd element to the end of the list.
7. Interchanging Elements
This type of list is used when the elements from the list have to be interchanged with the other element. This type will modify the existing element with a new one.
fruitsinterchange = ["cherry", "apple", "guava"]
fruitsinterchange = "orange"
In the above example, the elements printed in the output console are the interchanged order of elements whereas the original set of elements list did not have “orange” as an element. It has been replaced in place of “apple”.
8. Loop a list
This type is used when you want to see the elements inside the list. We can use loop statements to check the number of elements in the list. We can use if, while, for, etc. looping statements to print the elements.
fruits = ["guava", "orange", "papaya"]
for items in fruits:
print("fruit in the list is",items)
In the above example, the list of elements printed in the output console is the number of elements present in the list. The listing id running inside an if loop where the elements are printed one by one after the print statements.
9. Length of a List
This will print the length of the mentioned list and will print in the output console.
vegetables = ["chilli", "tomato", "potato"]
In the above code, vegetables are the name of the list and the length of the whole list is 3 as it holds three elements in it.
10. Adding an element in the list
This will add/append the data in the list. It will be added at the end of the list.
vegetables = ["chilli", "lemon", "tomato"]
In this article, we discussed a few characteristics and features about the lists in Python. We generally use this when we have a set of elements that need to be traversed back and forth. It also discussed different types of lists and their functionalities. It is useful it many business purposes/scenarios.
This is a guide to Python List. Here we discuss the introduction to Python Lists along with the top 10 types of Python Lists with Example. You can also go through our other suggested articles to learn more–