In this article, MarsDevs presents you with the technical information on Python list, its syntax, examples, and different operations on python list.
Although there are many data structures in Python, the sequence is the most basic one among them. The sequence starts from zero indexes and goes up to the length of sequence -1.
Python List is used for storing the sequence of different data types. A list is a mutable (can be changed or modified) data type which implies we can change the elements of a list even after they have been formed. We can perform many operations on the list like adding, subtracting, multiplying, slicing and comparing, etc.
Here is the example of List,
Syntax of List :
In the list, the elements are enclosed within square brackets () and separated by comma (,).
Here is the example of a list containing different types of data,
In the above example, the list contains different data types (like integer, floating point, string, boolean) all separated by commas and enclosed within square brackets.
Python List constructor takes a single argument which is nothing but the collection or sequence like tuples, string, set, and dictionary.
Here is the example,
In the above example, we can notice that the list() method changes the sequence from String and Tuple into List without altering the order of elements sequence.
Here we will compare two lists so as to know more about the return type of list.
In the above 2 examples, we can see both lists have the same number of elements but the ordering is different. The example in which both lists have the same elements and in the same order returns True while other examples return False.
Python has 6 data types that are used for storing the sequence but the list is the most common and stable among them.
Here are the different list methods
1. append() - This method will add a new element to the end of the list.
2. extend() - This method extends the list by adding all specified iterables like tuple, and list at the end of the list.
3. insert() - This method is used for inserting elements at the specified index.
4. remove() - This method is used to remove the first occurrence of the specified element from the list.
5. pop() - This method is used to remove the element at the specified index. If the index is not passed by default it will remove the last element of the list.
6. clear() - It will remove/delete all the elements of the list.
7. sort() - This method will sort the elements of the list in ascending order.
8. count() - This method will return the count of elements present in the given argument.
9. copy() - It will return the copy of the list.
10. reverse() - This method will reverse the index location of the elements in the list. The first element will be indexed last, the second with the second last, and so on.
11. max() - returns maximum in the list.
13. index() - returns index of first occurrence of a number.
14. len() - returns size or length of the given list.
Now let's discuss these methods in a more detailed way with the help of examples.
As discussed earlier python list is a collection of distinct types of elements like integer, string, and boolean, all enclosed within square brackets and elements separated by commas. A list can be empty and can have more than 1 element, there is also a concept of a nested list in which we can add a list inside another list. Like
We can access the elements of the list by using the index operator . In Python, the index starts from 0 and goes from up there, if there is a list containing 10 elements, then the index will range from 0 - 9. Also, if we try to access the element out of range then we get index out of range error. Only integers must be used as the index, otherwise, python will raise TypeError.
The elements of the nested list can also be accessed. The negative indexing is also supported by python, where the last element is represented by the index -1, the second last by -2, and so on.
In order to access the sub-list of elements in a list, we can use the slicing operator( : ). The syntax for slicing the list is as follows
list_name[start : stop : step]
Unlike String and Tuples, lists are mutable i.e they can be updated. We can use the assignment operator (=) to update a single element or a group of objects. The append() method is used to add a single element to the end of the list and extend() method is used to add several elements.
Moreover, we can combine or concatenate two lists using the + operator. Also, the * operator is used to repeat a list a given number of times.
The insert() method is also used to add elements at a certain index in the list. Its syntax is like
If we know the index of the element which we want to delete then we can use the del method, otherwise use the remove() method.
Suppose, we want to delete an entire list and then use the del method.
The remove() method is used to remove a specific element and pop() is used to get rid of an element at a specific index. If no position is passed in the pop() method, then it will remove and return the last element of the list by default.
A list comprehension is made of brackets that will hold the expression that is run for each element.