In this tutorial, we will learn about List in Python. The list is similar to arrays except one difference List can contain a heterogeneous type of data(Integer, Strings and Object) in a single list. In list, we add list element according to the index number. The first element of the list will always save on 0 index. Indexing of list is the same as indexing of array. It means each element of the list will have its own index number.
Page Contents
Initialization of List in Python:
List in Python can be initialize using square brackets []. We can place our list elements in these square brackets. List in Python doesn’t need a built-in function for the creation of the list. Check this example to understand it.
# declaring list
first_list = []
# simple list
first_list = [10, 15, 20]
# list with mixed datatypes
first_list = [10, "owlbuddy",5.5]
Nested List in Python:
When adding a List as an element in another list it is known as Nested List. Here we will see an example of Nested List.
# nested list
nested_list = [2,[1,2,3],5]
Access elements from a list:
Now it’s time to learn how we can access elements of List. There are two ways to access List elements like using List Index, using Negative Indexing etc. we will learn about each way in detail.
List Index:
We can access elements of a list using List index. List index would always Integer. List index starts from 0 and each element has its own index. For example, if a List has 10 elements it means the index of List will from 0 to 9.
#accessing elements of list
first_list = ['O','w','l','b','u','d','d','y']
# Output: O
print(first_list[0])
# Output: b
print(first_list[3])
Negative indexing:
In Python, we can access elements of List using negative indexing. It means the last index of List would have index -1 and second the last index of List would have index -2. Check this Illustration to understand it more clearly.
#accessing elements of list
first_list = ['O','w','l','b','u','d','d','y']
# Output: y
print(first_list[-1])
# Output: u
print(first_list[-4])
Adding Elements into Python List:
There are various ways to add elements in List like append() method, insert() method, extend() method. We will learn about each method. Let’s start with append.
Adding elements in List using append() method:
We normally use the append() method to add elements in List. With the help of the append() method, we can add a single element at a time in List. We can also add Tuples to List using this append() method. Check this example of the append() method.
#accessing elements of list
first_list = []
# adding elements in List using append() method.
first_list.append('O')
first_list.append('w')
first_list.append('l')
first_list.append('b')
first_list.append('u')
first_list.append('d')
first_list.append('d')
first_list.append('y')
# List after adding elements
print(first_list)
Adding elements in List using insert() method:
We use append() method to add an element in List at the last position of List. But if you want to add Element in List at the specific index you can use the insert() method. Check this example of the insert() method.
#accessing elements of list
first_list = [1,5,8,4,2]
# adding elements in List using insert() method.
first_list.insert(0,5)
first_list.insert(2,6)
# List after adding elements
print(first_list)
Adding elements in List using extend() method:
The extend() method is used to add multiple elements at a single time in List. Check this example.
#accessing elements of list
first_list = [1,5,8,4,2]
# adding elements in List using extend() method.
first_list.extend([15,14,18])
# List after adding elements
print(first_list)
Furthermore, we can also use + operator to add concatenate two Lists and we can use * operator to repeat List for multiple times. Check this example.
# adding two List using + operator.
first_list = [1,5,8]
second_list=[5,3,7]
# List after adding elements
print(first_list+second_list)
# Use of * Operator to show List multiple times
print(first_list * 2 )
Removing Elements from the List:
We can remove elements from List using various methods like remove() method, pop() method and del keyword. We can also use clear() method to clear while list at once. We will learn about each method in detail.
Deleting elements from List using remove() method:
We can use remove() method to delete an element from the string. Check this example.
first_list = [1,5,8,8,5,1,6,1,15,12,10,3,2]
# using remove method
first_list.remove(5)
# list after remove method
print(first_list)
# removing range of elements from List using remove method
for i in range(1, 3):
first_list.remove(i)
# list after remove method
print(first_list)
Deleting elements from List using pop() method:
first_list = [1,5,8,8,5,1,6,1,15,12,10,3,2]
# using pop method
first_list.pop(5)
first_list.pop(3)
# list after remove method
print(first_list)
Use of clear method in List:
We can use clear() method to remove all elements in List.
first_list = [1,5,8,8,5,1,6,1,15,12,10,3,2]
# using clear method
first_list.clear();
# list after remove method
print(first_list)
Some methods in Python to use with list object:
Method | Description |
---|---|
append() | This method is used to add an element at the end of the list. |
extend() | This method is used to add a list into another list. |
insert() | This method is used to add an element in the list at a specific index. |
remove() | This method is used to remove an element in the list. |
pop() | This method is used to remove an element from the specific index in the list. |
clear() | This method is used to remove all elements from the list. |
index() | This method returns to the first index of giving value. |
count() | This method returns the count of the number of items passed as an argument. |
sort() | This method uses to sort a list in ascending order. |
reverse() | This method uses to reverse order of elements in the list. |
copy() | This method returns a copy of the list. |