List in Python
What is List in Python
In Python, lists are used to store the multiple values in one variable. We can say that list is the collection of similar as well as different types of data. Python contains 4 built-in data types and the list is one among them. Lists are created using square brackets.
Suppose we want to store the age of 10 students in class, instead of creating the 10 different variables, we will create one list and in that list, we can store the age of all 10 students.

Example 1:
#Program of The list
age = [12, 13, 11, 15, 17, 16, 14, 11, 10, 15]
print(numbers)
Output:
[12, 13, 11, 15, 17, 16, 14, 11, 10, 15]
Example 2:
a= [4,"hello",12.6,'z']
print(a)
Output:
[4,"hello",12.6,'z']
Explanation:
In example 1, we have taken the same type of data in the list. But in example 2, we have taken different data types. As we know that list can store similar as well as different data.
What is an Index?
In the list, the index is the integer value of that location where the data is present in the list.
Example:
Names = [“Yashraj”, “Rohit”, “Akash”, “Shubham”]

Characteristics of List
- Lists are mutable, which means we can do changes in the list.
- We can access the elements of the list with the help of the Index.
- A list can store any kind of data.
- The list is ordered.
- The list is dynamic.
- The list can be nested.
Accessing List Elements
In a list, every element is associated with a number that number is known as the list index. To access the element from the list, we need the index number of that element. In the list, index number always starts with 0.
Example:
Names = ["Yashraj", "Rohit", "Akash", "Shubham"]
# accessing the item at index 0
print(Names[0])
# accessing the item at index 3
print(Names[3])
Output:
Yashraj
Shubham
Negative indexing in a list
In Python, we can give the negative indexing. The negative index starts from length to the -1, because 0 cannot be negative so it starts with the -1.
Example:
Names = [“Yashraj”, “Rohit”, “Akash”, “Shubham”]

Example:
Names = ["Yashraj", "Rohit", "Akash", "Shubham"]
# accessing the item at index 0
print(Names[-1])
# accessing the item at index 2
print(Names[-3])
Output:
Shubham
Rohit
Slicing in a list
In Python, it is possible to access particular items or elements from the list using the slicing operator.
Syntax:
slice(start, end, step)
- Start: Here, you need to mention the index from where you want to start your slicing.
- End: Here, you need to mention the index where you need to end the slicing
- Step: Here, you need to mention the step of slicing.
Example:
# List slicing in Python
A = ['h','e','l','l','o']
# elements from index 1 to index 3
print(A[1:3])
# elementsfrom index 3 to end
print(A[3:])
#elementsbeginning to end
print(A[:])
Output:
['e', 'l']
['l', 'o']
['h', 'e', 'l', 'l', 'o']
Explanation:
In above program,
- A[1:3] will return the list with an item from index 1 to index 3.
- A[3:] will return the list with items from index 3 till the end of the list.
- A[:] will return the total list.
Add an element to the list
In the list, we can add the element, which means if there are already some elements in the list then we can also add a new element to the existing list.
There is various methods to add the element in the list, one method is using append and we can do this using the extend method also.
Append()
In the append method, we will add the data at the end of the list.
Example:
A = [21, 11, 34, 44]
print("Before Append:", A)
# using append
A.append(32)
print("After Append:", A)
Output:
Before Append: [21, 11, 34, 44]
After Append: [21, 11, 34, 44, 32]
Extend()
Using the extend method; we will add one list to another list. Like if we have two lists then with the help of the extend function, we can convert the two lists into one list.
Example:
A1 = [22, 33, 55]
print("List1:", A1)
A2 = [43, 66, 88]
print("List2:", A2)
# join two lists
A1.extend(A2)
print("List after append:", A1)
Output:
List1: [22, 33, 55]
List2: [44, 66, 88]
List after append: [22, 33, 55, 44, 66, 88]