Introduction
Data structures are a very important in any programming language and python is no exception. They are of great importance when stroring and organizing data. In this article, we will have a top overview one of the most commonly used data structure in python, the list. We will be looking at what a list is, it's use cases, inbuilt methods and inbuilt operations. Ok let's dive in.
What is a list in python?
A list is a series of one or more items seperated by commas enclosed in square brackets.
my_list = [1,2,3,True,'string_elem',{3},(3,4),{'f':55}] # could also be created with the list(function)
It is a mutable data structure which means that it can be changed after it has been created unlike the python tuple.
my_list[1] = 'new_item'
print(my_list) # output -> [1,'new_item',3,True,'string_elem',{3},(3,4),{'f':55}]
It is also an ordered data structure which means the list retains the order of the items specified at the time of list creation unless explicitly modified.
my_list = [x for x in range(1,10)] # list comprehension to create a list of numbers 1 through 9 in that order
print(my_list) # output [1,2,3,4,5,6,7,8,9]
The items in a list can be of any data type. The items in a list are accessed using their index.
A list index is the position of an item in a list. The first item in a list has an index of 0, the second 2 and so on.
One could also use -1
in reference to the index of the last item , or -2
for the second last and so on.
The index of an item in a list can be accessed using the index()
method.
my_list.index(3) # returns 2
A list can not be used as a key in a dictionary i.e. it is unhashable. Trying to do this would throw an error.
methods and operations supported by a list
- adding or removing items from a list
- sorting a list
- reversing a list
- finding the length of a list
- finding the index of an item in a list
- finding the count of an item in a list
- finding the maximum and minimum item in a list
- finding the sum of items in a list etc etc
methods explained
my_list = ['x',3,5,True,('d')] # sample list to use for example code
adding or removing items from a list
- append() - adds an item to the end of a list
my_list.append('new_elem') print(my_list[-1]) # prints out 'new_elem'
extend() - adds multiple items to the end of a list
new_list = ['f','y','z'] my_list.extend(new_list) print(my_list) # prints out ['x',3,5,True,'new_elem','f','y','z']
- insert() - adds an item at a specified index
my_list.insert(3,'replaced') # replaces the item at index 3 with the string 'replaced'
- remove() - removes the first occurence of an item from a list
my_list.remove('x') # removes first occurence of x from the list
- pop() - removes an item from a list at a specified index and returns the item. If no index is specified, the last item is removed and returned.
- clear() - removes all items from a list
- del - removes an item from a list at a specified index
- append() - adds an item to the end of a list
sorting a list
- sort() - sorts a list in ascending order and does this in place.
my_list.sort() #would sort my_list in ascending order , return None
- sorted() - sorts a list in ascending order and returns a new list , the original list is left intact. To sort in descending order , set the key word argument
reverse
toTrue
.sorted_list = sorted_list(my_list,reverse=True) # returns a new list sorted in descending order and stores it in sorted_list
- sort() - sorts a list in ascending order and does this in place.
reversing a list in place
- reverse
my_list.reverse()
returns None
- reverse
finding the length of a list
- len
len(my_list)
returns an integer denoting length of the list
- len
finding the count of an item in a list
- count
my_list.count('elem_whose_freq_is_needed')
returns how often the element passed on as argument appears in the list.
- count
finding the maximum and minimum item in a list
- max
max(my_list)
returns integer denoting largest list element - min
min(my_list)
returns integer denoting smallest or lowest value of list
- max
finding the sum of items in a list
- sum
sum(my_list)
like the name suggests returns a sum of the elements in the list. - The methods
max, min, sum
work on lists filled with only numerical values. We could find more of the methods supported by the list using the python inbuilt function ,dir
i.e.dir(list_name)
.
- sum
Use cases
- storing a list of items with or without duplicates
- storing a list of items in a specific order
- storing a list of items with or without a specific data type
Wrapping up
In this article we have looked at what a list is, some of its use cases,the inbuilt methods and operations. We also looked at how to use the list methods and operations. This is only a small portion of what lists can be used for , more in depth information could be obtained from the official python documentation. I hope you found this article helpful. If you have any questions or comments, please leave them in the comment section below. Thank you for reading :).