What is Python List?
- The list is a compound data type that is mutable sequences.
- Among the other compound data types, the lists are the most versatile.
- The Python lists are generally used to store a collection of homogeneous items.
- Python has built-in functions that you may use to append, delete, sort, etc. items in the list.
- The lists can contain other lists, called nested lists.
- I will explain lists along with methods in detail with examples in the coming section; let me start with how to construct lists in Python.
Syntax for constructing lists
The list can be constructed in different ways:
A list can be a comma-separated values enclosed in brackets e.g.
- You may create an empty list by simply using brackets e.g. [].
- A list may contain mixed items like numbers and string, e.g.
A list may also be constructed by using list comprehension e.g.
By type constructor e.g.
lst = list() #It will create an empty list
lst = list(sequence)
The index of the list items starts at 0.
I will show you examples of constructing the list in different ways with code that you may execute in your own system as well.
A simple Python list example with for loop
In this example, a simple list of numbers is created. A for loop is used to get through that list and the print function is used to display list items.
Code:
lst_ex = [10,20,30,40,50] for x in lst_ex: print ("The current list item:",x)
Output:
Creating a list of strings example
Similarly, you may create a list of strings separated by commas as shown in the example below:
This is how a list of strings is created and a for..in loop is used to display its items:
# An example of constructing a list str_list = ['List','is','a','compound','data type','in', 'Python'] for str in str_list: print ("The current list item:",str)
Output:
A list built with mixed items example
A list may contain mixed items like numbers and strings as well. This example demonstrates how you may create a mixed list:
The mixed list is constructed as follows:
See the complete code and output below:
Python program:
# An example of constructing a list mixed_list = [1,'List',2,'is',3,'a',4,'compound',5,'data type',6,'in', 7,'Python'] for l_item in mixed_list: print ("The current mixed list item:",l_item)
Output:
The current mixed list item: List
The current mixed list item: 2
The current mixed list item: is
The current mixed list item: 3
The current mixed list item: a
The current mixed list item: 4
The current mixed list item: compound
The current mixed list item: 5
The current mixed list item: data type
The current mixed list item: 6
The current mixed list item: in
The current mixed list item: 7
The current mixed list item: Python
A demo of accessing specific items of a list
You may access specific list items by using the index of elements. The list index starts at 0. In this example, a list is created and only specific items are displayed by using indices.
This is how a list is created and items are accessed and displayed in above example:
mixed_list = [1,'List',2,'is',3,'a',4,'compund',5,'data type',6,'in', 7,'Python'] print (mixed_list[1], mixed_list[3], mixed_list[5], mixed_list[7],mixed_list[9],mixed_list[11],mixed_list[13])
The following is the output:
An example of creating a list by type constructor
As mentioned earlier, you may also create the lists by using the type constructor. See the following example of creating and displaying a list by using type constructor:
This is how the range function is used to create list items:
#An example of using type constructor a_list = list(range(5)) print (a_list)
Output:
A few Python List methods
List Method | Description |
---|---|
list.sort() |
Sort the list in ascending or descending order. |
list.append() , list.extend() , list.insert() |
These methods add items to a list. You can learn more about their differences by referring to the tutorial. |
list.pop() |
Used to remove items from the list. |
list.remove() |
Also used to remove elements from a list. |
list.reverse() |
Reverses the order of items in a Python List. |
len(list) |
Returns the total number of elements in a list. |
A few demos of using list built-in functions
Let us move ahead by using the built-in list functions like append, del, sort, len(), max, min, and others.
An example of using the len() method
The len() method is used to get the length or total number of elements in the list.
See this example where a list is created and the Python len() method is used:
The code:
lst = [1,'a',2,'b',3,'c'] print ("Total items in list = ", len(lst))
Output:
Removing list elements by del () and remove() methods
To remove the existing elements from the Python list, you may use the delete or remove methods.
Use the remove() method of the list if you want to remove by value. See the following example:
The code for removing the item:
lst = ['a','b','c'] print ("Total items before exectuing remove method = ", len(lst)) lst.remove('b'); print ("Total items after remove = ", len(lst))
Result:
The code:
lst = ['a','b','c'] print ("Total items before exectuing del method = ", len(lst)) del lst[1]; print ("Total items after del = ", len(lst))
Result:
Sorting a list Python example
The sort() method is used to sort a given list from lower to higher numbers or alphabetically.
See the example below where a list of random numbers is created. After that, the sort method is used to arrange items from lower to high numbers.
The list is displayed before and after using the sort method:
The code:
lst = [70, 80, 10.5, -5, 16] print ("A list before sorting:",lst) lst.sort() print ("The list after sort method:",lst)
Output:
The list after sort method: [-5, 10.5, 16, 70, 80]
Adding items by using append method
The code for adding a new item by append:
lst = [10,20,30,40,50] print ("A list before append:",lst) lst.append(60) print ("The list after append:",lst)
Output:
The list after append: [10, 20, 30, 40, 50, 60]
Adding items by using extend method
Rather than adding a single item by using the append method, you may use the extend method to add elements at the end of a list.
The extend method adds another list at the end of the specified list. See an example below where two lists are created:
The code:
lst1 = [10,20,30,40,50] lst2 = [60,70,80] print ("The lst1 before extend:",lst1) lst1.extend(lst2) print ("The list after extend:",lst1)
Output:
You can see, the items of lst2 are added at the end of lst1 by using the extend method.
Adding items by using Python List insert method
If you require adding the new item at a specific position in an existing list rather than at the end, you may use the insert method.
The insert method takes two parameters. One is to specify the index number and the other is the item to add. See a demonstration below:
The code to insert an item at the specific position:
lst1 = [10,20,30,40,50] print ("The lst1 before insert:",lst1) lst1.insert(2,25) print ("The list after insert:",lst1)
Output:
You see, an item, 25 is added after item 20. The new item is added to the given index number.