Python lists

A list in Python is a datatype that is used to store multiple values. A list can be formed by assigning different values inside square brackets and separated by commas.

Example

mylist = ["India", "France", "Australia", "Nepal"]
print(mylist)

Output:

[“India”, “France”, “Australia”, “Nepal”]

Items in list are ordered, indexed, and changeable.

Indexing of list

The list items are indexed, the first item has index 0, the second item has index 1 and so on.

Example

mylist = ["India", "France", "Australia", "Nepal"]
print(mylist[1])

Output:

France

In the above example, the index of India is 0, the index of France is 1, the index of Australia is 2 and the index of Nepal is 3. The value written inside the square bracket inside the print function is the index. Notice that writing list name followed by an index number inside square bracket would fetch the value at that index.

So by writing mylist[1], we would get the value at index 1, which is in our case – France.

List items are ordered

Lists are ordered, which means we cannot change the position of elements once the list is created. If a new element is added, it will be added to the last.

List allows duplicate values

A list can have items with the same value.

Example

mylist = ["India", "France", "Australia", "Nepal", "France"]

Notice that we have the same value ‘France’ in the list, although at different positions.

Length of List

To find out the length of a list, we can use the len() function.

Example

mylist = ["India", "France", "Australia", "Nepal"]
print(len(mylist))

Output:

4

Data types of list

A list can be of any data type. It can contain strings, integer values, or Boolean values.

Example

mylist1 = ["India", "France", "Australia", "Nepal"]
mylist2 = [11, 1, 4, 7, 40]
mylist3 = [True, False, False, True]

A list can also contain values of different types.

Example

mylist = ["India", 1, True, 4]

The type() function

A list is defined as an object with the data type ‘list’.

Example

mylist = ["India", "France", "Australia", "Nepal"]
print(type(mylist))

Output:

<class ‘list’>

The list() constructor

A list can be created by using the list() constructor as well.

mylist = list(("table", "chair", "cup", "plate"))
print(mylist)

Output:

[“table”, “chair”, “cup”, “plate”]

Adding new item at the end of the list

The append() function adds a new item to the end of a list.

Example

mylist = ["India", "France", "Australia", "Nepal"]
mylist.append("Singapore")
print(mylist)

Output:

[“India”, “France”, “Australia”, “Nepal”, “Singapore”]

Adding item at a specific index

The insert() function adds a new item to the list at a specific index.

Example

mylist = ["India", "France", "Australia", "Nepal"]
mylist.insert(2, "Singapore")
print(mylist)

Output:

[“India”, “France”, “Singapore”, “Australia”, “Nepal”]

Removing item from a list

The remove method is used to remove an item from a list.

Example

mylist = ["India", "France", "Australia", "Nepal"]
mylist.remove("Australia")
print(mylist)

Output:

[“India”, “France”, “Nepal”]

However, if we want to remove an item through an index, we can use the pop() function.

Example

mylist = ["India", "France", "Australia", "Nepal"]
mylist.pop(1)
print(mylist)

Output:

[“India”, “Australia”, “Nepal”]

If we don’t specify the index, the function will remove the last item from the list.

Example

mylist = ["India", "France", "Australia", "Nepal"]
mylist.pop()
print(mylist)

Output:

[“India”, “France”, “Australia”]

The del keyword

The del keyword can also be used to remove an item from a specified index.

Example

mylist = ["India", "France", "Australia", "Nepal"]
del mylist[2]
print(mylist)

Output:

[“India”, “France”, “Nepal”]

If you don’t specify the index, the del keyword will delete the whole list.

Example

mylist = ["India", "France", "Australia", "Nepal"]
del mylist

The clear() function

The clear function empties the list. That means the list still remains, however with no content.

Example

mylist = ["India", "France", "Australia", "Nepal"]
mylist.clear()
print(mylist)

Output:

[]