List in Python

In this tutorial, we will learn about the list in Python. We will discuss how to create, slice, insert, update and remove an element from Python list with example.

What is the List

  • The List is a sequence data type in Python.
  • The List can store different types of data.
  • The List can have any number of items.
  • Each item of the list has an index number, starting from 0.
  • The List is mutable(we can change the value of a list).

How to Create a List

We can create a list by pacing all items inside square brackets [ ] separated by comma (,).

#Empty List, contain nothing
n=[]

#List with numbers
n=[11, 22, 33, 44, 55]

#List with Text
color=["Red", "Green", "Yellow"]

#List with mixed items
n=[1, 2, "Hello", [99, 88]]

Access Element of the List

We can access the element of the list by referring index number in square [ ] brackets.

#creating a list
n=[11, 22, 33, 44, 55]

#print all the items
print(n)

#print first item of the list
#index of the first item is 0
print(n[0])

#index of the second item is 1
print(n[1])

#index of the last item is -1
print(n[-1])

#index of the second last item is -2
print(n[-2])

Slicing List in Python

We can access the sublist of a list using a slicing operator ( : ) in square brackets [ ]. Syntax of the slicing is [start : stop : step].

n=[11, 22, 33, 44, 55, 66, 77, 88]

#print all the elements
print(n)
print(n[:])

#print first to fifth element (index 0 to 4)
print(n[0:5])

#print element of index 2 to 4
print(n[2:5])

#print element of index 2 to last
print(n[2:])

#print element of index 2 to second last
print(n[1:-1])

#print value of index (0,2,4,6)
print(n[0:8:2])

Modify Lists in Python

Lists are mutable, so we can change or update the values of the list. We can use an index of the list to change the items.

Using the List’s Index

n=[11, 22, 33, 44, 55]
print(n)

#change first and third value
n[0]=99
n[2]=100
print(n)

#output : [99, 22, 100, 44, 55]

Using the List’s Slicing

n=[11, 22, 33, 44, 55]
print(n)

#change 2nd to 4th item
n[1:4]=[1, 2, 3]
print(n)

#output : [11, 1, 2, 3, 55]

Adding Two List

list1 = [1, 2, 3]
list2 = [9, 8, 7]
list3 = list1 + list2
print(list3)

#output : [1, 2, 3, 9, 8, 7]

Modify List using Methods

1. append() – Add a single item to the end of the list and update the list. Syntax: list.append( item )

n=[11, 22, 33, 44, 55]
print(n)

#using append() method
n.append(99)
print(n)

#output : [11, 22, 33, 44, 55, 99]

2. extend() – It can add multiple items to the list at the end. Syntax: list1.extend( list2 )

n=[11, 22, 33, 44, 55]
print(n)

#using extend() method
list1=[1, 2, 3]
n.extend(list1)
print(n)

#output : [11, 22, 33, 44, 55, 1, 2, 3]

3. insert() – It can insert a new item at the specific index, rest items will shift to the right. Syntax: list.insert( index, item )

n=[11, 22, 33, 44, 55]
print(n)

#using insert() method
n.insert(1,99)
print(n)

#output : [11, 99, 22, 33, 44, 55]

Delete an Item from the List

In Python, we can delete items from the list using Python’s method remove(), pop(), clear() or we can use del keyword.

del Keyword in Python

We can delete one, more or complete list using del keyword in Python. In Python del keyword uses index or slice to remove items.

#delete first item
n=[11, 22, 33, 44, 55]
del n[0]
print(n)    #output: [22, 33, 44, 55]

#delete multiple items
n=[11, 22, 33, 44, 55]
del n[1:4]
print(n)    #output: [11, 55]

#delete entire list
n=[11, 22, 33, 44, 55]
del n
print(n)
#output: error, n is not define
#we remove entire list

Delete List using Python’s Methods

  • remove() – In Python, remove() method delete given item.
  • pop() – It removes the last item of the list.
  • pop(index) – It removes an item of the given index.
  • clear() – Remove all the items of the list.
#delete an item by value
n=[11, 22, 33, 44, 55]
n.remove(22)
print(n)    #output: [11, 33, 44, 55]

#delete last item of the list
n=[11, 22, 33, 44, 55]
n.pop()
print(n)    #output: [11, 22, 33, 44]


#delete an item by index
n=[11, 22, 33, 44, 55]
n.pop(2)
print(n)    #output: [11, 22, 44, 55]

#delete all items
n=[11, 22, 33, 44, 55]
n.clear()
print(n)    #output: [ ]

Useful Functions of the List

  • count() – Return count of how many times an element is in the list.
  • index() – It returns the index of the first matching element
  • sort() – sort the list in ascending or descending order.
  • reverse() – It reverses the list.
n=[11, 22, 11, 88, 33, 11, 55]
print(n)
print(n.count(11))  #count an item in the list
print(n.index(33))  #return index of 33 in the list

#sort the list in ascending
n=[11, 22, 11, 88, 33, 11, 55]
n.sort()
print(n)

#sort the list in descending
n.sort(reverse=True)
print(n)

#reverse the list
n=[11, 22, 11, 88, 33, 11, 55]
n.reverse()
print(n)

Aggregate Function

  • len() – Return length of the list.
  • min() – Return minimum value of the list.
  • max() – Return maximum value of the list.
  • sum() – Return sum of the values of the list.
#aggregate function
print(len(n))
print(min(n))
print(max(n))
print(sum(n))

107
Created on By S. Shekhar

Python – Quiz 13

1 / 4

 For list, n=[1, 2, “Hello”, [99, 88]]. len(n) is______.

2 / 4

Which of the following statements is not true for extend()?

3 / 4

n=[11, 22] Which of the following will delete 22 from the list?

4 / 4

Which of the following is true for count( )?

Your score is

The average score is 55%

0%