Updated May 3, 2023
Introduction to Python List
Python list is a number of elements/ data gathered under a single place and can be of any data type, including the integers, characters, floats, etc. The list allows duplication in the data, and Python provides a number of operational options that can be applied to the list elements. There are many types of lists in python, like negative indexing, index range, beginning range, ending range, interchanging elements, length of a list, a loop of the list, and addition of elements.
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 needs 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.
Code:
listnew = ["papaya", "watermelon", "guava"]
print(listnew[2])
Output:
In the above code, the list new is the name of the list, and listnew[2] 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 item’s corresponding index number.
Code:
ABC = ["guava", "papaya", "banana"]
print(ABC[0])
Output:
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 [0] will print the first element of the list.
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 a negative integer, i.e. -1,-2, etc.
Code:
listabc = ["papaya", "watermelon", "guava"]
print (listabc[-1])
Output:
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.
Code:
listnew = ["papaya", "banana", "guava", "watermelon", "litchi", "apple", "cherry"]
print(listnew[2:5])
Output:
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 until the mentioned index, i.e., the element’s index number.
Code:
listfruits = ["apple", "banana", "cherry", "orange", "kiwi”, "lemon"]
print(listfruits[:4])
Output:
The elements printed in the output console are the elements present before the mentioned index in the above code. 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.
Code:
fruits = ["apple", "melon", "cherry", "custardapple", "kiwi", "dragonfruit"]
print(fruits[3:])
Output:
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.
Code:
fruitsinterchange = ["cherry", "apple", "guava"]
fruitsinterchange[1] = "orange"
print(fruitsinterchange)
Output:
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.
Code:
fruits = ["guava", "orange", "papaya"]
for items in fruits:
print(items)
print("fruit in the list is", items)
Output:
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.
Code:
vegetables = ["chilli", "tomato", "potato"]
print(len(vegetables))
Output:
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 to the list. It will be added at the end of the list.
Code:
vegetables = ["chilli", "lemon", "tomato"]
vegetables.append("potato")
print(vegetables)
Output:
Conclusion – Python List
In this article, we discussed a few characteristics and features of 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.
Recommended Articles
This is a guide to Python List. Here we discuss a brief overview along with the top 10 types of Python Lists with Example. You can also go through our other suggested articles to learn more –