Hit enter after type your search item

Python List Data Type

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.

I will explain lists along with its methods in detail with examples in the coming section; let me start by 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.

lst = [1,2,3,4,5]

  • You may create an empty list by simply using brackets e.g. [].
  • A list may contain mixed items like numbers and string, e.g.

lst = [1,’a’,2,’b’]

  • A list may also be constructed by using list comprehension e.g.

lst = [x for x in iterable]

  • 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 print function is used to display list items.

The list is constructed as follows:


After that, a for loop is used to display list items:

Output:

Python list

Creating a list of strings example

Similarly, you may create a list of string separated by commas as shown in the example below:

This is how a list of string is created and a for..in loop is used to display its items:


Output:

string list

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:

mixed_list = [1,’List’,2,’is’,3,’a’,4,’compound’,5,’data type’,6,’in’, 7,’Python’]

See the complete code and output below:

Python program:


Output:

 

mixed list

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:


The following is the output:

List is a compound data type in Python

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 range function is used to create list items:


Output:

list constructor

A few Python List methods

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 numbers of elements in the list.

See this example where a list is created and Python len method is used:

The code:


Output:

list length

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:


Result:

list remove

If you want to delete by index number, then use del method as shown in the example below:

 

The code:


Result:

list delete

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:


Output:

list sort

Adding items by using append method

You may add new items by using the append method in an existing list. The append method will add a new item at the end of the list. See a demo below:

The code for adding a new item by append:


Output:

list append

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:


Output:

list extend

You can see, the items of lst2 are added at the end of lst1 by using the extend method.

Adding item by using Python List insert method

If you require adding the new item at a specific position in an existing list rather at the end, you may use the insert method.

The insert method takes two parameters. One 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:


Output:

list insert

You see, an item, 25 is added after item 20. The new item is added to the given index number.

This div height required for enabling the sticky sidebar