Introduction
Python lists are like dynamic size arrays, and here it's not mandatory to declare all the values homogeneous. It can contain different data types together, like integer, float, strings, etc.
Lists contain comma-separated values between two square brackets.
Example of List
lang = ['Python', 'Java', 'C++', 1999, 2022]
Declaration of a List
Declaration of a list is pretty simple. As you can see in the example above, the declaration is also the same.
lang = ['Python', 'Java', 'C++', 1999, 2023]
print(lang)
Output
['Python', 'Java', 'C++', 1999, 2023]
Another way to declare a python list is using the 'list()' constructor.
lang = list(('Python', 'Java', 'C++', 1999, 2022))
print(lang)
Output
['Python', 'Java', 'C++', 1999, 2022]
There is one more way to declare a list. Here, you can call the 'list()' class and store it in a variable. By default, it will create an empty list if you don't pass anything to it.
lang = list()
print(lang)
Output
Ways to access List Elements
Python offers several ways to access list elements. We will cover each of them in a single program below.
lang = ['Python', 'Java', 'C++', 'HTML', 'JavaScript']
# 1st element
print(lang[1])
# 0th element
print(lang[0])
# -1 refers to the last element
print(lang[-1])
# starting and ending ranges are given
print(lang[1:3])
# 0th to 3rd element
print(lang[:3])
Output
Java
Python
JavaScript
['Java', 'C++']
['Python', 'Java', 'C++']
Add elements to the list
There are three built-in methods to add elements to a python list.
1. append(): It is used to add the new elements at the end of the existing items.
2. insert(): It helps to add items in a specified position. In this case, we have to mention the position inside.
3. extend(): As the name refers, it is used to extend a list with elements from another list. You can think of this as the concatenation operation between two Python lists.
lang = ['Python', 'Java', 'C++', 'HTML', 'JavaScript']
# Add data to a list using append method
lang.append("Kotlin")
print(lang)
# Adding an element to the list at the 2nd position
lang.insert(2, "C")
print(lang)
lang2 = ["Ruby"]
# Concatenating two lists: Simple Way
lang3 = lang + lang2
print(lang3)
# Add one list(lang2) at the end of the another list(lang)
lang.extend(lang2)
print(lang)
Output
['Python', 'Java', 'C++', 'HTML', 'JavaScript', 'Kotlin']
['Python', 'Java', 'C', 'C++', 'HTML', 'JavaScript', 'Kotlin']
['Python', 'Java', 'C', 'C++', 'HTML', 'JavaScript', 'Kotlin', 'Ruby']
['Python', 'Java', 'C', 'C++', 'HTML', 'JavaScript', 'Kotlin', 'Ruby']
Remove elements from a Python List
There are some built-in methods to remove single or multiple items from a python list.
1. pop(): By default, it removes the last item but, we can specify the index number too.
2. remove(): It is used to remove a specified item from a list. In this case, we specify the item name directly instead of the index number.
3. del(): Th 'del()' method can be used in two ways. If you mention the index number of an item, it will remove that specified item from the list. If you don't mention anything, it will delete the entire list.
We will see the usage of each function above in the program below.
lang = ['Python', 'Java', 'C++', 'HTML', 'JavaScript', 'Kotlin']
# By default it will remove the last item.
lang.pop()
print(lang)
# Here, we are mentioning the index number.
lang.pop(1)
print(lang)
# Removing the specified item
lang.remove("C++")
print(lang)
# Removing the item from the 1st position
del lang[1]
print(lang)
# It will delete the entire list
del lang
Output
['Python', 'Java', 'C++', 'HTML', 'JavaScript']
['Python', 'C++', 'HTML', 'JavaScript']
['Python', 'HTML', 'JavaScript']
['Python', 'JavaScript']
Count the number of elements in a Python List
There are two methods to count the number of elements in a python list.
1. len(): It returns the total number of elements present in a list or the length of the list.
2. count(): It tells the number of presence of a specific item in a list.
Let's see the programming example below.
num = [4, 3, 1, 7, 5, 9, 3, 2, 1, 7, 9, 8, 0, 8]
print(f"The length of the list is: {len(num)}")
print("The Number of the presence of 8:",num.count(8))
Output
The length of the list is: 14
The Number of the presence of 8: 2
Find the index number of a data item from a list
Python offers to find the index number of a data item from a list by using the index() method. The below example is for you.
lang = ['Python', 'C++', 'C', 'C#', 'Java', 'Ruby', 'Python']
# Assigning the data item to a variable
val = 'Java'
# printing the index no. of the given item
print(f"Item: {val}\nIndex no. {lang.index(val)}")
Output
Item: Java
Index no. 4