A list is a data structure in python which can store sequence of values. It is like an array of other programming languages.

A list is constructed with brackets [ ] and commas (,) separating every element in the list.

A list in python has the following properties:

  • Mutable – Unlike strings lists are mutable, means the elements inside a string can be changed.
  • Ordered – Item in list is inserted in the ordered fashion.
  • Accessible through Index – Each item in a list can be accesed by its corresponding index.

Create a List

We can easily create a list using square brackets [].

list() Constructor

list() in python converts the others data types into a list object.

Length of List

len() function will tell you how many items are present in the list

Access list item

An item in a list can be accessed using the index inside the square brackets.

Note that indexing in a list starts from 0.

To grab other items except for the first item you can append colon “:” after second index value.

Negative list Indexing

Negative indexing of a list allows accessing the value in the backward fashion.

For example, l[-1] refers to the last item in the list, l[-2] refers to second last item in the list and so on.

output

Add an Item to List

To add a new item to the list you can use append(). Make sure to pass the value to the append method.

If you do not want to use the append() method then you can use the following shortcut.

Change an Item in List

To change item value simply assign a new value in the corresponding index of the old item.

Delete an Item from List

There are two major ways to delete or remove an item from a list.

Remove item by Index

del() and pop() are the two method to delete an item by index. Just pass the index value and corresponding item from the list get removed.

The only difference is between both the methods is that if no index is passed to pop(), then it simply removes and returns the last value of the list.

Whereas del() requires the index value and it does not reuturn the value after deletion.

output

Remove an Item by Value

remove() method of list object removes the first occurrence of the item from the list. Use it when you are not sure where a particular item is in the list.

Remove all Items

Use clear() to remove all items and completely empty the list.

No need to pass anything to clear() method.

Check if item exists in a list

To check whether an item exists in a list use “in” and “not in” operator with python if statement

Iterate through a List

To iterate through a loop you can use for loop

output

If you need to update the items of the list then loop through the indexes of the list using range() and len() as described below.

Quick Revision

Basic List Methods

  • sort() – Sort the values of the list in ascending order.
  • count(x) – Count the frequency of ‘x’ in the list.
  • append(x) – Add ‘x’ to the list.
  • pop() – Remove last element from the list.
  • reverse() – Reverses the list.
  • index(x) – Returns the index of element ‘x’.
  • remove(x) – Removes first appearance of ‘x’ from the list.
  • clear() – Removes all elements from the list.

Leave a Reply