Python Tutorial
Python - Introduction Python - Syntax Python - Variable Python - Number Python - Casting Python - String Python - Operators Python - Conditions Python - Loops Python - List Python - Tuple Python - Set Python - Dictionary Python - Function Python - Module Python - Date & Time Python - Input Output Python - Error & Exception Python - File Handling
Python OOP
OOP - Introduction OOP - Class Members OOP - Constructor Destructor OOP - Data Hiding OOP - Inheritance OOP - Overriding OOP - Overloading

Python List

There are four collection data types in the Python programming language:

  • List - is an collection which is ordered and changeable. Allows duplicate members.
  • Tuple - is a collection which is ordered and unchangeable. Allows duplicate members.
  • Set - is a collection which is unordered and unindexed. No duplicate members.
  • Dictionary - is a collection which is unordered, changeable and indexed. No duplicate members.

When choosing a collection type, it is useful to understand the properties of that type. Choosing the right type for a particular data set could mean retention of meaning, and, it could mean an increase in efficiency or security.


Creating List

The list is the most versatile datatype available in Python, which can be written as a list of comma-separated values (items) between square brackets. Important thing about a list is that the items in a list need not be of the same type.

Creating a list is as simple as putting different comma-separated values between square brackets.

It is also possible to use the list() constructor to make a list:


Accessing Values in Lists

To access values in lists, use the square brackets for slicing along with the index or indices to obtain value available at that index.


Updating Lists

You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add elements in a list with the append() method.



Delete List Elements

To remove a list element, you can use either the del keyword if you know exactly which element(s) you are deleting.

You can also use the remove() method if you do not know exactly which items to delete.


Merging two lists

You can merge two or more lists using + operator:


Unpacking lists

Unpacking list is a way to assign list items to variables in just one line of code:


Multi-dimensional list

A multi-dimensional list behaves like a table, data is stored in rows and columns.

You can create multi-dimensional list as given below:


Python Built-in List Functions

Function Description
len(list) Gives the total length of the list.
max(list) Returns item from the list with max value.
min(list) Returns item from the list with min value.
list(seq) Converts a tuple into list.

Python Built-in List Methods

Method Description
list.append(obj) Appends object obj to list
list.count(obj) Returns count of how many times obj occurs in list
list.extend(seq) Appends the contents of seq to list
list.index(obj) Returns the lowest index in list that obj appears
list.insert(index, obj) Inserts object obj into list at offset index
list.pop(obj = list[-1]) Removes and returns last object or obj from list
list.remove(obj) Removes object obj from list
list.reverse() Reverses objects of list in place
list.sort([func]) Sorts objects of list, use compare func if given