Python List
Python

Python List

Editorial Team
Editorial Team

Follow us on Discord or Telegram to get access to free tutorials and updates.

In Python, a list is an ordered collection of items of different data types. The items of a list are enclosed in [ ] brackets. Each list items are separated by a comma (,).

mylist = ['Lemon', 'tea', 123, 3.14]

Each item in the list will have an index. The index of the first element in the list will be 0.

Accessing list items

You can access the Python list items in the following ways:

  1. Display all list items using print() method.
  2. Using the index of the list item.
  3. Using a for loop

1) Displaying list items using print() method

You can display the elements of a list by specifying it in the print() method.

mylist=['Radius','Circle',3.14]
print(mylist)

Output

['Apple', 'Banana', 3.14]

2) Accessing list items using index number

Every elements in a list can be accessed using its index number.

mylist=['Radius','Circle',3.14]
print(mylist[0])
print(mylist[2]

Output

Radius
3.14

3) Accessing list items using for loop

A for loop can be used to access the elements of a list.

mylist=['Radius','Circle',3.14]
for i in mylist:
	print(i)

Output

Radius
Circle
3.14

Changing the values of list items

The value at any index in a list can be changer as shown below:

mylist = ['Radius','Circle',3.14]
mylist[1] = 'Diameter'
print(mylist)

Output

['Apple', 'Banana', 3.14]

Adding list items

You can add items to the list using append() function or insert() function.

The append() function adds new element to the end of the list. To insert new element at the specified index, use insert() function.

mylist=['C++','C Sharp']
print(mylist)
#Adding list items
mylist.append('Java')
print(mylist)
#Inserting to a specific position
mylist.insert(0,'C')
print(mylist)

Output

['C++', 'C Sharp']

['C++', 'C Sharp', 'Java']

['C', 'C++', 'C Sharp', 'Java']

Removing list items

You can remove items from the list using any of the following methods.

  1. Using remove() method.
  2. Using pop() method.
  3. Using clear() method.
  4. Using del keyword.

Using remove() method - The remove method can be used to remove the specified item from the list. If the specified item exists in the list multiple times, the last occurrence of the item will be removed.

mylist=['C++','C Sharp','C Sharp','Java']
mylist.remove('C Sharp')
print(mylist)

Output

['C++', 'C Sharp', 'Java']

Using pop() method - The pop() method removes the item at the specified index. If no index is specified, last item from the list is removed.

mylist=['C++','C Sharp','C Sharp','Java']
mylist.pop(0)
print(mylist)
mylist.pop()
print(mylist)

Output

['C Sharp','C Sharp','Java']
['C Sharp','C Sharp']

Using clear() method - This method clears the list.

mylist=['a','b','c']
mylist.clear()

This program when executed will produce the following output.

[]

Using del keyword - The del keyword can be used to remove an element at specified index. It can also be used to delete the entire list.

a=['a','b','c']
del a[0]
print(a)

This program when executed will produce the following output:

['b','c']

You can delete the list using the del keyword as shown below. Executing this code will produce an error because del keyword deletes the list.

a=['a','b','c']
del a
print(a)