Beginner Level
Intermediate Level
Advanced Level
Introduction
Lists are one of the most commonly used data types in Python and are incredibly useful when it comes to storing and manipulating data. A list is essentially a collection of items that are ordered and can be accessed by their index. In Python, a list can contain a mixture of data types including strings, integers, and even other lists. This tutorial will cover the basics of creating, accessing, and manipulating lists in Python. By the end of this tutorial, you will have a solid understanding of lists and be able to use them effectively in your Python programs. So let's get started!
Table of Contents :
- Lists in Python
- Accessing elements of List
- Change elements of a list
- Adding new element to the list
- Removing element from a list
- Removing element using del keyword
- Removing element using pop() method
- Removing element using remove() method
Lists in Python :
- An ordered collection of items is called a List in Python.
- List data type in python is implemented through the
list
class. - In Python square brackets are used to indicate a list.
- Items are enclosed within square brackets to create a list.
- A list contains one or more items separated by comma.
- An empty list can also be created in Python as :
empty_list = []
- A list can contain items of different data types.
- A list can also contain other lists as its elements.
- On printing the list Python prints it along with square brackets.
- Different basic operations possible on lists are :
- Accessing elements of list.
- Modifying the elements of list
- Adding new elements in the list
- Removing elements from the list
- Code Sample :
my_list = [1,2,3,4]
print(f"Value of my_list = {my_list}")
print(f"type of my_list = {type(my_list)}")
# Output
# Value of my_list = [1, 2, 3, 4]
# type of my_list = <class 'list'>
Accessing elements of List :
- List is an ordered collection i.e. each element has a unique index.
- The indexes of lists is zero based.
- The first item of a list in Python has an index = 0, the second item has index = 1
- The elements of a list in Python can be accessed using their index.
- The syntax of accessing an element in Python is : list_name[index]
- In Python we can access elements using negative index as well.
- The negative index accesses elements from the end of the list e.g.
todo_list[-1]
returns the last element of todo_list.todo_list[-2]
returns the second last element of todo_list.
- Code Sample :
my_list = [1,2,3,4]
print(f"The first element of my_list = {my_list[0]}")
print(f"The second element of my_list = {my_list[1]}")
print(f"The last element of my_list = {my_list[-1]}")
print(f"The second last element of my_list = {my_list[-2]}")
# Output
# The first element of my_list = 1
# The second element of my_list = 2
# The last element of my_list = 4
# The second last element of my_list = 3
Change elements of a list :
- The elements of a list can be changed by assigning new value to it.
- The new value is assigned using this syntax :
list_name[index] = new_value
- Code Sample :
my_list = [1,2,3,4]
print("mylist = ", my_list)
# Changing element
my_list[2] = 100
print("mylist = ", my_list)
# Output
# mylist = [1, 2, 3, 4]
# mylist = [1, 2, 100, 4]
Adding new element to the list :
- In Python the in built
append()
method can be used to add new elements to a list. - Using the
append()
method new elements are added to the end of the list. - To add a new element at a particular position we use the
insert()
method. - Code Sample :
my_list = [1,2,3,4]
print("mylist = ", my_list)
# Adding element
my_list.append(100)
print("mylist = ", my_list)
# Inserting element
my_list.insert(2, 200)
print("mylist = ", my_list)
# Output
# mylist = [1, 2, 3, 4]
# mylist = [1, 2, 3, 4, 100]
# mylist = [1, 2, 200, 3, 4, 100]
Removing element from a list :
- An element can be removed from a list in python by using :
- del keyword
- pop() method
- remove() method
Removing element using del keyword :
- We can remove an element from a list in Python by using the del keyword.
- we need to specify the index of the element while deleting.
- The syntax for deleting an element from list is as follows :
del list_name[index]
- Code Sample :
my_list = [1,2,3,4]
print("mylist = ", my_list)
# deleting using del keyword
del my_list[2]
print("mylist = ", my_list)
# Output
# mylist = [1, 2, 3, 4]
# mylist = [1, 2, 4]
Removing element using pop() method :
- The pop method can be used to remove the last element of the list.
- Also the
pop()
method returns the last element after removing it. - Usually the pop method is used when we want to use the value of the last element and then remove it after using.
- The
pop()
method can also take index as a parameter to remove and return an element from a particular position. - Code Sample :
my_list = [1,2,3,4]
print("mylist = ", my_list)
print()
# using pop method
e = my_list.pop()
print("mylist = ", my_list)
print("Popped element : ", e)
print()
# using pop method with index
e = my_list.pop(1)
print("mylist = ", my_list)
print("Popped element : ", e)
# Output
# mylist = [1, 2, 3, 4]
# mylist = [1, 2, 3]
# Popped element : 4
# mylist = [1, 3]
# Popped element : 2
Removing element using remove() method :
- The
remove()
method is used to remove element by value. - Code Sample :
my_list = [1,2,3,4]
print("mylist = ", my_list)
print()
# using remove method
my_list.remove(3)
print("mylist = ", my_list)
# Output
# mylist = [1, 2, 3, 4]
# mylist = [1, 2, 4]
Prev. Tutorial : Docstrings
Next Tutorial : Iterating a list