What are lists in Python? How to create a list, how to add and remove list elements?

Lists are extremely powerful way of storing information in Python. Each list in Python can contain hundreds to tens of thousands of elements, each of which is separated by a comma. 

Further, these List elements can be of one or more types - text, numbers, alphanumericals, even lists and tuples as well.

Lists in Python are denoted by square brackets [ ] in between which all the elements of lists are contained.

Example list -   fruits_name = ['apple', 'mango', 'litchi', 'guava', 'orange']

Here, we have a list named fruits_names which has 5 elements, each of the element is enclosed within inverted commas and each element is separated by a comma. All elements together are enclosed within square brackets, representing that to be a list.

Lists is dynamic in nature which means that elements of lists can be modified - existing elements can be removed and new elements can be added to the list.

Side note - Tuples are similar to lists but are represented by brackets instead of square brackets. There are few more differences which we will discuss in Tuples section.

To create a new list in Python, simply write a meaningful variable name (for example - cars_model, followed by equal to '=' followed by a pair of square brackets [ ].

This creates a empty list named cars_model.

cars_model = []

print(cars_model)

>> [] #represents list with no elements

To add a new element to the list, use addend function. 

For example - cars_model.append('Volvo')

To check how the list is updated, use print(cars_model).

This results in the output - ['Volvo']

cars_model.append('Audi')

cars_model.append('BMW')

cars_model.append('Maruti')

cars_model.append('TATA')

cars_model.append('Land Rover')

Now, print the list - print(cars_model).

The result would be - ['Volvo','Audi',BMW','Maruti','TATA','Land Rover']

Here, the list has 6 elements. 

To check the length of the list, use the len function. print(cars_model.len())

In Python lists, the position or index of the list starts from 0 instead of 1.

To access the elements of Python lists, type the name of Python list along with the index of the element that you want to access. 

For example - print(cars_model[1]) will print out the 2nd element, therefore the value Audi

Similarly,  print(cars_model[4]) will print the 5th element, which is TATA.

To remove an element from a Python list, there are several ways - 

  • To remove a specific value from a Python list, use the remove function.
    • print(cars_model.remove('BMW')) #this will output ['Volvo','Audi','Maruti','TATA','Land Rover']
  • To remove the last element from Python list, use pop()
    • print(cars_model.pop()) #this will output ['Volvo','Audi','Maruti','TATA']
  • To remove an element from a specific index of a Python list, use pop('index number')
    • print(cars_model.pop(2)) #this will output ['Volvo','Audi','TATA']

Related tutorials -

  • How to install Spyder for using Python?
  • How to install Anaconda for using Python?
  • How to create a list in Python?
  • How to add elements to a list in Python?
  • How to sort a Python list?
  • How to sort a Python list temporarily?
  • How to remove elements from a Python list?
  • How to print the Python list in reverse order?
  • How to access the Python list elements from the end?
  • How to print a range of elements from a Python list?
  • How to do slicing of Python lists?
  • What are some of the frequently used functions for Python lists?