Python List

Suppose you have 5 cars. If you want to assign an individual name to them, you may want to use 5 different variables of string type, like:

car1 = 'Honda'
car2 = 'Toyota'
car3 = 'Chevrolet'
car4 = 'Jeep'
car5 = 'Ford'

In this case we can use a single variable to hold all these values called List. In Python square bracket [ ] is used to create a list. Our cars list will be:

cars = ['Honda', 'Toyota', 'Chevrolet', 'Jeep', 'Ford']
print(cars)

output:
['Honda', 'Toyota', 'Chevrolet', 'Jeep', 'Ford']

Note: We also can use list() constructor to make a list. Like:

Example:

cars1 = list(('Honda', 'Toyota', 'Chevrolet', 'Jeep', 'Ford'))
print(cars1)

Output :
['Honda', 'Toyota', 'Chevrolet', 'Jeep', 'Ford']

Accessing list elements: 

If we have n numbers of elements in a list, index/position of the first element will be 0 and last element will be n-1.

So, if we want to get the first car name, we can simply use cars[0] . Let us print the first car name:

cars = ['Honda', 'Toyota', 'Chevrolet', 'Jeep', 'Ford']
print(cars[0])

Output:
Honda

Print the second car name:

cars = ['Honda', 'Toyota', 'Chevrolet', 'Jeep', 'Ford']
print(cars[1])
Output:
Toyota

Print the last or 5th car name:

cars = ['Honda', 'Toyota', 'Chevrolet', 'Jeep', 'Ford']
print(cars[4])
Output:
Ford

Note: In our car list has only string type. But a List can have different types of data.

Example:

myList = [1,3,4,'Honda', 1.3, 'Jeep']
print(myList[4])

Output:
1.3

Find Index of a particular list item

index() method is used to find the index of a particular list item.

cars1 = list(('Honda', 'Toyota', 'Chevrolet', 'Jeep', 'Ford'))
print(cars1.index('Ford'))

Output: 4

Finding the length of the list:

To find the number of elements in a list or the length of the list, we can use len() method.

cars = ['Honda', 'Toyota', 'Chevrolet', 'Jeep', 'Ford']
print(len(cars))

Output:
5

Looping through a list

It’s a very common operation to loop through a list and accessing the items in it. We will learn more about looping while studying different types of looping.
Example: In this example we are going to fetch all the list elements using for loop

abcList = ['d', 'c', 'a', 'e', 'b']
for item in abcList:
 print(item)

Output:
d
c
a
e
b

Changing a value in the list

If we want to change a particular element in a list, we have to use index of the element:For updating the second index value in our cars list: cars[1] = ‘Audi’

cars = ['Honda', 'Toyota', 'Chevrolet', 'Jeep', 'Ford']
cars[1] = 'Audi'
print(cars)

Output:
['Honda', 'Audi', 'Chevrolet', 'Jeep', 'Ford']

Adding a new element in a list

Adding a new element at the end of the list:

append() method is used to add/append an element at the end of the list.

cars = ['Honda', 'Toyota', 'Chevrolet', 'Jeep', 'Ford']
cars.append("BMW")
print(cars)

Output:
['Honda', 'Audi', 'Chevrolet', 'Jeep', 'Ford', 'BMW']

Adding a new element at a particular position of the list:

insert() method is used to add a new element at a particular position of the list. For inserting a new element in the 3rd position:

cars = ['Honda', 'Toyota', 'Chevrolet', 'Jeep', 'Ford']
cars.insert(2,'Mercedes-Benz')
print(cars)

Output:
['Honda', 'Audi', 'Mercedes-Benz', 'Chevrolet', 'Jeep', 'Ford', 'BMW']

Delete an element from the list

Delete a particular element from a list:

remove() method is used to delete a particular element from a list.

Example: Deleting ‘Jeep’ from our car list:

cars = ['Honda', 'Audi', 'Mercedes-Benz', 'Chevrolet', 'Jeep', 'Ford', 'BMW']
cars.remove('Jeep')

Output:
['Honda', 'Audi', 'Mercedes-Benz', 'Chevrolet', 'Ford', 'BMW']

Deleting an element from a particular index of the list:

pop() method is used to delete a particular index element from a list if we provide the index value, otherwise it will delete the last element of the list.

cars = ['Honda', 'Audi', 'Mercedes-Benz', 'Chevrolet', 'Ford', 'BMW']
cars.pop(1)
print(cars)


Output:
['Honda', 'Mercedes-Benz', 'Chevrolet', 'Ford', 'BMW']

We can also use del keyword to delete an element from a particular index:

Example:

cars = ['Honda', 'Mercedes-Benz', 'Chevrolet', 'Ford', 'BMW']
del cars[1]
print(cars)

Output:
['Honda', 'Chevrolet', 'Ford', 'BMW']

Note: del keyword is also used to delete the entire list

Example:

cars = ['Honda', 'Chevrolet', 'Ford', 'BMW']
del cars

Output:
NameError: name 'cars' is not defined

Clearing a list

To delete all the list items or make the list empty, we can use clear() method

Example:

abcList1 = ['d', 'c', 'a', 'e', 'b']
abcList1.clear()
print(abcList1)

Output:
[]

**Note: as we are able to add, change and delete elements from a list, list in python is mutable

Copying a list

we copy a list to make a new list .

copy() method to copy a list:

cars = ['Honda', 'Chevrolet', 'Ford', 'BMW']
carsNew = cars.copy()
print(carsNew)

Output:
['Honda', 'Chevrolet', 'Ford', 'BMW']

list() method to copy a list:

cars = ['Honda', 'Chevrolet', 'Ford', 'BMW']
carsNewUpdated = list(cars)
print(carsNewUpdated )

Output:
['Honda', 'Chevrolet', 'Ford', 'BMW']

Count list elements

If you want to count how many times a list element exists in the list, you can use count() function

cars1 = list(('Honda', 'Toyota', 'Chevrolet', 'Jeep', 'Ford', 'Jeep'))
print(cars1.count('Jeep'))

Output: 2

Sorting a list

We can sort the items of a list in a specific order ( Ascending or Descending).

sort() method can be used to sort the items of a list.
Example: Sort a list in ascending order

abcList = ['d', 'c', 'a', 'e', 'b']
abcList.sort()
print("After sorting: ", abcList)

Output:
After sorting:  ['a', 'b', 'c', 'd', 'e']

Example : Sort a list in descending order

abcList = ['d', 'c', 'a', 'e', 'b']
abcList.sort(reverse=True)
print("After sorting in descending order: ", abcList)

Output: 
After sorting in descending order:  ['e', 'd', 'c', 'b', 'a']

** Here reverse=True is used to say the sort() method to perform a descending order sorting

For sorting a list, you can also use python built-in sorted() method.
Example: Sort in ascending order

abcList = ['d', 'c', 'a', 'e', 'b']
print("Sorting ascending order using sorted: ", sorted(abcList))

Output:
Sorting ascending order using sorted:  ['a', 'b', 'c', 'd', 'e']

Example : Sort in descending order

abcList = ['d', 'c', 'a', 'e', 'b']
sortedList = sorted(abcList,reverse=True)
print("Sorting descending order using sorted: ", sortedList)

Output:
Sorting descending order using sorted:  ['e', 'd', 'c', 'b', 'a']

Note: sorted() method does not change the structure of the main list, it perform a sort operation and returns an iterable list. In-case of sort() method, it changes the structure of the list.

Reversing a list

If you only want to reverse a list, you can use reverse() method

Example: 
abcList1 = ['d', 'c', 'a', 'e', 'b']
abcList1.reverse()
print('Reversed list: ', abcList1)

Output:
Reversed list:  ['b', 'e', 'a', 'c', 'd']

In summary, commonly used Python list methods are:

Function/MethodDescription
index()find the index of a particular list item
insert()add a new element at a particular position of the list
append()add/append an element at the end of the list
len()find the number of elements in a list or the length of the list
remove() method is used to delete a particular element from a list
pop() method is used to delete an item from a particular index of a list if we provide the index value, otherwise it will delete the last element of the list
copy() we copy a list to make a new list .
count()counts how many times a list element exists in the list
sort()We can sort the items of a list in a specific order ( Ascending or Descending)
reverse()reverse a list
clear()delete all the list items or make the list empty

See our all the courses and lessons. Also see code examples in our GitHub repository.

Follow us on social media

Author