The list data type is exclusively used in Python. You can use this to store multiple values in a single variable. This data type is very similar to array in c/ c++ but instead of holding a single type, simultaneously it can hold multiple different types of value such as number, string, float, etc.
In this article, you will learn Python list data type and operations that can be performed on it with some examples.
Examples of Python list
The examples of Python list are given below –
# The list x contains only numbers x = [1, 2, 3 , 4, 5 ] # The given list y contains only strings y = ['a', 'b', 'c', 'd', 'e'] # The list z contains mix of numbers and string z= [1, 2, "abc",4, 'Hi'] # You can also use list inside a list list_2d= [[3, 2, 1], [6, 5, 4], [9, 8, 7]]
List items
The items of a list are ordered and changeable it allows duplicate values. Python allows indexing of list items i.e. the first item has an index 0
similarly second has index 1
and so on.
You can also use negative indexing the last item of the list has an index -1
similarly second the last item has -2
and so on.
Display the list
You can use the print function to print a list in Python.
Accessing list items using index
You can access items of a list using indices. Now see the example below.
x=[1, 2, 3, 4, 5] print(x[0]) print(x[1]) print(x[-1])
Now you can see the output of this program in the given image –
Changing an existing list item
If you want to change an item of a list simply assign a new value at that index for example –
x=[1, 2, 3, 4, 5]
To replace the item at index 2 with value ‘three’ use –
x[2] = 'three'
Now you can display the list using –
print(x)
See the output in the image below –
Adding data to a list
There are a few methods available that can be used to add items to a list.
For example –
Lets we have the given list –
fruits=['mango', 'banana', 'grapes']
Adding list item with append() method
The append() method will add the item at the end of the list. To add ‘apple’ to the list named fruits we will use the following statement.
fruits.append('apple')
To see the updated list item use the print function-
print(fruits)
Adding list items with insert() method
The insert method takes two arguments i.e. index and object. The index indicates where the item is to be inserted. For example to add another item ‘orange’ at the index 1
use –
fruits.insert(1, 'orange')
Now you can see the complete program which shows the usage of append and insert methods-
Concatenating two lists
To concatenate two or more lists you can use the +
operator. Suppose we have two lists i.e. fruit1 and fruit2 as given below –
fruit1 =['mango', 'banana'] fruit2=['papaya', 'orange' ] # Concatenate two lists fruits=fruit1+fruit2 print(fruits)
You can see the output in the image below –
Deleting data of a list
There are a few methods that are used to delete the list items.
Delete list items with the item name
You can use the remove() method to delete list items with their name.
Suppose we have a list with the name fruits –
fruits=['mango', 'banana', 'grapes', 'orange']
To delete banana from fruits list use the remove method which is given below –
fruits.remove('banana')
If there are multiple items with the same name then it will remove the item which comes first.
Remove list items using index
Using the pop() method you can delete the items by their index now to delete the item at the index 2
in fruits list use –
fruits.pop(2)
If you do not pass the index then it will remove the last item from the list.
Alternatively, you can use Python del operator. For instance to delete the item at the index 2
use –
del fruits[2]
Delete all list items
By using the clear() method you can delete all the items of a list. For example to delete all the items of fruits list use –
fruits.clear()
Iterate over a list
Using in operator with for loop you can iterate over list items. By using len() function you can find the length of a list then you can use a loop that iterates from the index 0
to the length-1
.
For example –
x=[1, 2, 3, 4, 5] for i in x: print(i)
OR use while loop, as given below, to iterate through the list
x=[1, 2, 3, 4, 5] i=0 while i < len(x): print(x[i]) i=i+1
The given loops will print the items of list x.
Sorting the list items
The sort() method is used to sort list items in ascending order or alphanumerically by default. You can use the keyword reverse=True
to sort the list in descending order.
For example-
fruits =['orange', 'apple', 'grapes', 'cherry'] numbers=[5, 3, 9, 6, 1, 0] # Now to sort lists fruits and numbers use fruits.sort() numbers.sort() print(fruits) print(numbers) # OR to sort these lists in reverse order use fruits.sort(reverse=True) numbers.sort(reverse=True) print(fruits) print(numbers)
You can see the output of this in the given image –
Python split and join methods
Python split() method converts a string to a list. For example –
str = " Hi, My name is Vinay, I am 22 years old " x = str.split(',') print(x)
This will split the comma-separated strings to list elements. You can see the output in the given image –
Python join() method is opposite to split() method. It converts a list to a string.
For example –
x =['Hi', 'My name is Vinay', 'I am 22 years old'] str = ','.join(x) print (str)
This will join the elements of list x and assign it to string str. Now see the output in the given image.
Like the sort(), split(), join() methods there are so many other useful methods that you can use with a list. A detailed list of these methods is given here.
Now if you have a query on the Python list then write to us in the comments below.