Python List | Create, append, remove items from list for beginner tutorial
In this python tutorial for beginners, we are going to explain about python list. This tutorial will help you mastering the lists in python perfectly. We will learn about what is the list? how it is used in python? how we can add or delete the elements in the list and many more. First, we will know what is a list in python?
Let's get started.
What is a List?
Python gives us a wide range of data types. The list is one of the useful data types that help you in storing multiple items in a single variable. It is written inside square brackets dividing the elements using a "comma". Also, the items in the list can be of different types which is a major advantage of the list. To use the list we are going to use the following syntax.
Syntax:
list_name= [ 'item1', 'item2', 'item3'.....]; #Some common examples of the list in python are:
cars= ['BMW', 'Ford', 'Honda', 'Jaguar'];
bikes= ['Hero', 'BMW', 'Ducati', 'Yamaha'];
|
Advantages of List Items in python
List items come with different advantages. They are ordered, changeable, and also allows duplicate values or items. Let's know in brief about all these features.
-
Ordered
The lists are ordered means the items in the list have a defined order and that order will not change. In short, if you will place new items or elements in the list they will be placed at the end of the list.
-
Changeable
The list is always changeable which means we can easily add, remove or change the items in the list after creating it.
-
Allow Duplicates
The list also allows duplicates that means the items in the list may have the same value. Look at the below example for an explanation.
Program:
list1 = ["BMW","Honda","Hero","Suzuki","Honda"]
print(list1)
|
Output:
['BMW', 'Honda', 'Hero', 'Suzuki', 'Honda']
Now as we know about what is a list in python. Let's learn how to create a list in python.
How to Create a List in Python
To create your first list in python we will program a small code that will create a list and output the list on the screen. Let's program
Program:
#To create an empty list first
list1= []
#Add items in the list
list1 = ['Bike', 'Car', 2020, 1990]
#Print list using print function
print(list1)
|
Output:
['Bike', 'Car', 2020, 1990]
Here we have used the square brackets to define the list. The items in the list are separated by commas. But the question is how we will access the elements of the list? Let's explore this.
How to access elements from the list?
In python, we can easily access the elements from the list. It is quite simple to print any item from the list using python. To fetch any item from the list we specify the index number of items inside '[]' square brackets. Let's know about this in brief with code.
Program:
# Create list
my_list = ['p', 'y', 't', 'h', 'o', 'n']
# It will print the Output: p
print(my_list[0])
# It will print the Output: t
print(my_list[2])
# It will print the Output: o
print(my_list[4])
# To create a Nested List
n_list = ["List in", [1, 2, 30, 4]]
# Nested indexing
print(n_list[0][1])
print(n_list[1][3])
|
Output:
p
t
o
i
4
Access Elements using Negative Indexing
Python also allows negative indexing in its data types. The index number -1 refers to the last item of the list and -2 refers to the second last item of the list and so on. to explain this we are going to have this program.
Program:
# To create list with items
my_list = ['a','b','c','d','e']
# Negative indexing in the list
print(my_list[-1])
print(my_list[-5])
|
Output:
e
a
Update Elements in The List
As the list is changeable we can easily add or remove elements in the list after creating it. To update the elements to the list we will use the append() method that helps in updating an item in the list. We just have to do this by updating an item of a given index number. Here is the program to do this
Program:
# Create a list with different items
list = ['Honda', 'Yamaha', 200, 150];
# Print the initial value of the index which you need to change
print ("Initial Value at index 2 is: ")
print (list[2])
# Update the value indexed at 2
list[2] = 2001;
# Print the new value
print("New value at index 2 is: ")
print(list[2])
|
Output:
Initial Value at index 2 is :
200
New value at index 2 is:
2001
Removing Elements From the List
We can also remove the element from the list as it is changeable. To do this we are going to use the del statement. If you already know which element you want to remove just use the remove() method. For example-
Program:
# Create a list with elements
list1 = ['Honda', 'BMW', 200, 150];
# To print the initial list
print (list1)
# Delete an element using del statement
del list1[2];
# TO print the updated list
print ("After deleting value at index 2 : ")
print (list1)
|
Output:
['Honda', 'BMW', 200, 150]
After deleting value at index 2 :
['Honda', 'BMW', 150]
How to Calculate List Length
We can also calculate the length of the list in python. To do this we will use the len() function that helps us in calculating the length easily in python. Here is the program to calculate the length of the list.
Program:
#Create a list with values in it
list1 = ["BMW", "Maruti", "Suzuki", "Honda"]
# Use the len() function to calculate the length of the list.
print(len(list1))
|
Output:
4
List Items - Data Types
Items in the list can be of any data type. It can be of string, int, boolean, etc. For example.
How to get the type of list in python
list1 = ["physics", "chemistry", "biology"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
print(list1)
print(list2)
print(list3)
|
Output:
['physics', 'chemistry', 'biology']
[1, 5, 7, 9, 3]
[True, False, False]
Also, a single list can contain different data types. Have a look at this code.
Program:
list1 = ["Physics", 24, True, 10, "Male"] |
Output:
['Physics', 24, True, 10, 'Male']
type()
In python we can identify the type by using type() method. For example list are defined as objects with data type 'list'. We can check the type by using this method. Here is a program to specify this.
Program:
# To make a list with different items
list1 = ["Physics", "Chemistry", "Biology"]
# Using type() method to identify the type
print(type(list1))
|
Output:
<type 'list'>
Using list() constructor to create a list
Also, you can use the list() constructor to create a new list. To explain this let's have a look at this program.
Program:
# Inside the constructor we have used the double rounded brackets to define list
list1 = list(("BMW", "Mercedes", "Honda"))
# To print the list
print(list1)
|
Output:
['BMW', 'Mercedes', 'Honda']
Here we have completed all the subtopics of List in python. Make sure to practice more coding related to this topic to master it. Here are a few examples of python you should try
Example 1:
# Create a list
even = [2, 4, 6]
# To add some new elements in the list
print(even + [8, 10, 12])
# Repeat the same element 4 times
print(["python"] * 4)
|
Output:
[2, 4, 6, 8, 10, 12]
['python', 'python', 'python', 'python']
Example 2:
# Program to Delete list items
list1 = ['p', 'y', 't', 'h', 'o', 'n']
# To delete only one item from the list
del list1[2]
print(list1)
# To delete multiple items from the list
del list1[1:5]
print(list1)
# To delete the whole list
del list1
|
Output:
['p', 'y', 'h', 'o
', 'n']
['p']
Example 3:
# Create a list of numbers
list1 = [1, 20, 43, 24, 45, 16, 37]
# To print the list items from 2nd to 3rd
print(list1[1:3])
# To print the list items from starting to 3rd
print(list1[:3])
# To print the list items from 4th to end of the list
print(list1[3:])
# To print the Whole list
print(list1[:])
|
Output:
[20, 43]
[1, 20, 43]
[24, 45, 16, 37]
[1, 20, 43, 24, 45, 16, 37]
Related Topics
Python Arrays and Different type of Array Examples