Python Lists

In this tutorial you will learn about the Python Lists and its application with practical example.

Python Lists

Python comes with a great built-in sequence data type known as “List”. List is a mutable ordered series of comma-separated values, each of the value as termed as Item. A list variable is defined by having values separated by commas and enclosed within square brackets ([]). A list can hold values of different data types, it means single list can contain strings, integers, as well as objects.

Lists are mutable hence very flexible data type, it mean values or items in a list can be added, removed, and changed any time. Lists are great to deal with data structures like stacks and queues.

Creating Lists

Python list can be defined by simply putting comma-separated values enclosed within square brackets ([ and ]).

Syntax1:-

A list having no elements is called an empty list.

Lists can also be created using the built-in list type object as following –

Syntax2:-

Here, sequence can be any kind of sequence object or iterable. You can also pass in another list, which will makes a copy. Tuples and generators can also be used as sequence object to create a lists.

Syntax3:-

Here, expression is evaluated for every item in the sequence to create list items.

In Python, if you assign a list to a variable, python interpreter will never creates a new list for that variable instead both will point to same list object.

Indexing Lists

Items in the list “colors” are indexed as following –

‘red’ ‘green’ ‘blue’ ‘white’ ‘yellow’
0 1 2 3 4

Negative indexing for the items in the list “colors” can be seen as following –

‘red’ ‘green’ ‘blue’ ‘white’ ‘yellow’
-5 -4 -3 -2 -1

Accessing Lists

An list elements can be accessed easily by index or subscript like list_name[index]. Indexing for any lists starts from zero (0) to the last element of a list is list_name[size-1] where size is the number of elements in the list.

Syntax:-

Example:-

here, L[i] returns the item at index i (Indexing for any lists starts from zero (0))

here, L[i:j] returns a new list, containing the items between index i and j; here i represents the start index and j represents the end index. If the specified index is outside the list, Python raises an IndexError exception.

Example:- List elements can be accessed in numerous way as following –

Output:-

Delete Item from a List

An items from a list can removed simply using the del statement. This will remove the value at the given index. The del statement can be used to delete a single or multiple items from the given list, it can also be used to delete entire list.

Example:-

Output:-

Iterating Through a List

The for-in statement makes it easy to loop over the items in a list:

If you need both the index and the item, use the enumerate function:

If you need only the index, use range and len:

Updating Lists

As Python lists are mutable, you can easily update an individual items or slices of the given list.

Syntax:- dfgdg

Example:- gdgdfg

Output:-

you can update a list slice as following –

Example:-

Output:-

If you want to add one item to a given list or several items to a list, it can be done using append() and extend() method respectively.

Slicing Lists

In Python, you can also extract a sub list from a given list. It can be done as following –

Syntax:-

Here, a sub list is extracted from given list starting from start_index and stopping just before end_index. The default values for start_index and the end_index is zero(0).

If you omit the start_index (before the colon), the slice starts from the beginning of the list. If you omit the end_index, the slice goes to the end of the list. If you omit both, it will return a copy of the given list.

Example:-

Output:-

In this tutorial we have learn about the Python Lists and its application with practical example. I hope you will like this tutorial.