Lists in Python

Lists act as containers, it can hold or store multiple data at the same time.

Lists are changeable and are ordered sequence of elements.

To know more about Python Lists you can visit the official Python Lists documentation.

To declare a list using [ ] brackets.

The elements inside the [ ] are the values

Python

# Creating an empty list called "list"
lst = [ ]
# Adding values called elements inside of the list
lst = [ 1, 2, 3, 4, 5]
# Printing the list
print(lst)

Output

[1, 2, 3, 4, 5]

Note: You should not use the variable name “list” as this is a reserved word

To access individual elements enter the variable name follows by a square bracket which has a number referring to which element you want.

Note lists start with index 0

For example:

lst = [ 1, 2, 3, 4, 5]
print("The first output for lst[0] is: ",lst[0])
print("The first output for lst[1] is: ",lst[1])

Output:

The first output for lst[0] is: 1
The first output for lst[1] is: 2

Note if you get the error “IndexError: list index out of range”

You tried to get an element that is not in the list

You can get a set of elements by adding a start:stop in the square bracket.

For example we might want to take a sub list with the numbers 2,3,4

We are starting with index 1 and finishing at index

For example:

lst = [ 1, 2, 3, 4, 5]
print("The output for lst[1:4] is: ",lst[1:4] )

output

The output for lst[1:3] is: [2, 3, 4]

Lists can also hold and contain strings

For Example:

lst = ["apple","banana","cherry"]
print(lst)

How to Add elements to a list in Python

Method 1 (The append method):

This is used to add one element to the end of a list

list name .append(item)

Example:

lst = [ 1, 2, 3, 4, 5]
lst.append(6)

print(lst)

output

[ 1, 2, 3, 4, 5,6]

Method 2 (The extend method)

This method allows you to add multiple items to the end of the list

list name.extend([items in square brackets separated by commas ])

Example:

lst = [ 1, 2, 3, 4, 5]
lst.extend([6,7,8])
print(lst)

output

[ 1, 2, 3, 4, 5,6,7,8] 

Method 3 (The concatenate method)

This method is very similar to the extend method

This is used to concatenate lists together

list name = list name + list of new items

With this method you can add multiple items

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

print(lst)

lst = lst + [6,7,8]

output

[ 1, 2, 3, 4, 5,6,7,8]

This can be shortened to:

lst = [ 1, 2, 3, 4, 5]
lst += [6, 7,8]
print(lst)

Method 4 (The insert method)

The insert method allows you to add an item at the specified index

list name.insert(The index(location and the item you want to add separated by a comma)

We will try to add 3 in its place in the list [1,2,4,5]

To do this we need to find the index we want to place our item

index 0 is 1

index 1 is 2

index 2 is 4

We want to place the item in index 2 so we will use lst.insert(2,3)

Example:

lst = [ 1, 2, 4, 5]
lst.insert(2,3)
print(lst)

Output

 [ 1, 2, 3, 4, 5]

Method 5 (The slice method)

If you specify a range using slice and assign another list or tuple, all items will be added.

lst = [ 1, 2, 3, 4, 5] lst[1:1] = [100, 200, 300] print(lst)

Output

 [1, 100, 200, 300, 2, 4, 5]

You can also replace the original item. All items in the specified range are replaced.

lst = [ 1, 2, 3, 4, 5]
lst[1:2] = [100, 200, 300]
print(lst)

Output 

[1, 100, 200, 300, 3, 4, 5]

Credit

https://note.nkmk.me/en/python-list-append-extend-insert/

How to Remove items from a list in Python

Method 1 (The clear method)

This method will clear all the items in a list

List name.clear()

Python

lst = [ 1, 2, 3, 4, 5]
lst.clear()
print(lst)

Output

[]

Method 2 (the pop method)

You can remove an item at a specified index

list name.pop(index)

In the lst [ 1, 2, 3, 4, 5] we want to remove the number 3

The number 3 is in index 2 so we will use lst.pop(2)

Python

lst = [ 1, 2, 3, 4, 5]
lst.pop(2)
print(lst)

Output

[ 1, 2, 4, 5]

You can remove the last item if you do not pass a parameter into pop

Example

lst = [ 1, 2, 3, 4, 5]
lst.pop()
print(lst)

Output

[1, 2, 3, 4]

Method 3 (The remove method)

If you do not want to remove using the index you can remove by using the item name 

We can use list name .remove(Item name)

Example

lst = [ 1, 2, 3,-1, 4, 5]
lst.remove(-1)
print(lst)

Output

[ 1,2,3, 4, 5]

Method 4 (The del method)

Allows you to remove a sub list from a list

del lst[start of sublist index : end of sublist index]

If we had lst [1,200,300,400,2,3,4,5] and we wanted to remove items 200 300 and 400 

We need to know the index of 200 and 400

The index of 200 is 1

The index of 400 is 3

Therefore the start is 1 and the end is 4, we can write del lst[1:4]

Python

lst = [1,200,300,400,2,3,4,5]
del lst[1:4]
print(lst)

Output

[ 1,2,3, 4, 5]

Credit

https://note.nkmk.me/en/python-list-clear-pop-remove-del/

Replacing an item in a list

To replace an item in a list you will need the index of the item you want to replace

list name[index] = new item

If we had lst=[ 1,2,3, 4, 5] and we wanted to replace the number 3 with the number 6

The index of 3 is 2 so

lst[2] = 6

Example

lst=[ 1,2,3, 4, 5]
lst[2] = 6
print(lst)

Output

[ 1,2,6, 4, 5]

Credit: https://pythoner.name/en/exchange-list-items

Getting the total length of a list in Python

To count how many items there are in total in your list use

len(list name)

Example

lst = [ 1,2,3, 4, 5]
print(len(lst))

Output

5

Get the index of an item in a list in Python

If you wanted to find the index of the first occurrence of an item you can use list name .index()

If you wanted to find the index of 2 in lst [ 1,2,3, 4, 5] you can use lst.index(2)

lst = [ 1,2,3, 4, 5]
print(lst.index(2))

Output

1

Count the number of times an item is in your list

list name.count(item)

if we wanted to count the number of 2’s in lst [10, 7, 7, 1, 3, 4, 4, 3, 8, 6, 2, 4, 7, 8, 9, 3, 1, 4, 9, 6, 9, 2, 2, 6, 4, 2, 2, 2, 9, 10, 9, 6] we can use lst.count(2)

Example

lst = [10, 7, 7, 1, 3, 4, 4, 3, 8, 6, 2, 4, 7, 8, 9, 3, 1, 4, 9, 6, 9, 2, 2, 6, 4, 2, 2, 2, 9, 10, 9, 6]

print(lst.count(2))

Output

6

Reverse a list in Python

If you want to reverse a list you can use list name.reverse()

Example

lst = [10, 7, 7, 1, 3, 4]
lst.reverse()
print(lst)

Output

[4, 3, 1, 7, 7, 10]

Sorting a list in Python

To sort a list in ascending order (from small to big) you can use the list name.sort() function

Example

lst = [10, 7, 7, 1, 3, 4]
lst.sort()
print(lst)

Output

[1, 3, 4, 7, 7, 10]

If you want to reverse a list in descending order you can pass the parameter reverse=True into sort

Example

lst = [10, 7, 7, 1, 3, 4]
lst.sort(reverse=True)
print(lst)

Output

[10, 7, 7, 4, 3, 1]

The .sort() method changes the original list. If we do not want to change the original list we can use sorted(list name)

Example

originalList = [10, 7, 7, 1, 3, 4]
newList = sorted(originalList)
print("The original list is :", originalList)
print("The new list is :",newList)

Output

The original list is : [10, 7, 7, 1, 3, 4]
The new list is : [1, 3, 4, 7, 7, 10]

The sorted method can also take in the parameter reverse=True

Example

originalList = [10, 7, 7, 1, 3, 4]
newList = sorted(originalList,reverse=True)
print("The original list is :", originalList)
print("The new list is :",newList)

Output

The original list is : [10, 7, 7, 1, 3, 4]
The new list is : [10, 7, 7, 4, 3, 1]