To begin with, let's know this first - What are Lists in Python?
Lists in Python are a collection of elements of various data types like integer, string, float etc. Once initialized, the elements in a list can be changed or modified. Also, Lists in Python are ordered and have a definite count. Now, let's understand Lists better by looking at its operations.
List creation is pretty simple in Python. You can place all your elements/items separated with commas
, inside a square bracket
[ ] and that forms a list.
Here is an example.
#List with no elements list1 = [ ] print(list1) #list with single element list2 = ["Python"] print(list2) # list with multiple elements of different data types list3 = ["Python", 15, 10.5] print(list3) Output: ['Python'] ['Python', 15, 10.5]
Note: A list can have another list as an element/item inside it. Such a list is called as Nested list.
The elements/items in the list can be accessed using the index operator
[ ]. List index starts from 0. So, if a list has 10 elements, then its index will start at 0 and end at 9. If we try to access an element that is out of the index range, then the compiler will give you an Index error. Also, the index value must be an integer only.
Similarly, elements of a nested list can be accessed using nested indices.
# accessing elements in a list list1 = [30, 20.2, "Python"] print(list1) # accessing elements in a nested list list2 = [, [20.2, "Python"]] print(list2) print(list2) Output: 20.2 30 Python
Similar to Strings in Python, Lists can also be accessed both forward and backward. Backward indexing is also called as negative indexing as the index starts at -1. The above examples are based on forward indexing, now, let us look at how the same items can be accessed using negative indexing.
# accessing elements in a list list1 = [30, 20.2, "Python"] print(list1[-2]) # accessing elements in a nested list list2 = [, [20.2, "Python"]] print(list2[-2]) print(list2[-1][-1]) Output:20.2 30 Python
In Python, a range of elements in a list can be accessed using the slicing operator
: and this technique is called Slicing in List.While using this slicing operator, you need to mention the starting index & ending index of values to be sliced and the values till the position (ending index - 1) get sliced.
#slicing a list list1 = [10, 20.3, 29, "Welcome", "Hai"] print(list1[0:1]) print(list1[1:4]) Output:  [20.3, 29, Welcome]
Explanation: Here, the starting index value 1 is inclusive and the ending index value 4 is exclusive.
Unlike Strings & Tuples, Lists are mutable. This means their elements can be easily modified or new elements can be easily added to a list.To modify a list element or a range of elements, we can simply assign a new value to it using its index value. For example,
list1 = [5, 16.3, 345, "Python", "FACE Prep"] #modifying one element in the list list1  = 18.1 print (list1) #modifying range of elements in the list list1 [1:5] = [18.1, 987, "I am", "the best"] print (list1) Output: [5, 18.1, 345, 'Python', 'FACE Prep'] [5, 18.1, 987, 'I am', 'the best']
We can add one or more elements/items to a list at a time. To add one element, we can use the
append () method and to add several items, we can use the
extend () method.
list1 = [5, 16.3, 345, "Python", "FACE Prep"] #adding one element at a time list1.append (18.1) print (list1) #adding several elements at a time list1.extend (["I love Coding", 25, 19.6]) print (list1) Output: [5, 16.3, 345, 'Python', 'FACE Prep', 18.1] [5, 16.3, 345, 'Python', 'FACE Prep', 18.1, 'I love Coding', 25, 19.6]
The syntax to insert one element into a list is insert (index value, element to be inserted)and in case you want to insert several elements, then simply put them in an empty list. For instance,
list1 = [5, 16.3, 345, "Python", "FACE Prep"] #adding one element at a time list1.insert (1, 18.1) print (list1) #adding several elements at a time list1 [1:1] = ["New Element", 3, 87.1] print (list1) Output: [5, 18.1, 16.3, 345, 'Python', 'FACE Prep'] [5, 'New Element', 3, 87.1, 18.1, 16.3, 345, 'Python', 'FACE Prep']
del keyword can be used to delete one element, several elements,or the entire list.
list1 = [5, 16.3, 345, "Python", "FACE Prep"] #deleting one element del list1 print (list1) #deleting several elements del list1[1:5] print (list1) #deleting entire list del list1 print (list1) Output: [5, 345, 'Python', 'FACE Prep']  Traceback (most recent call last): File “main.py”, line 13, in print (list1) NameError: name ‘list1’ is not defined
Explanation: In the last step, we deleted the entire list and then tried to print it, hence it throws an error. This means our list got deleted and the program executed correctly.
Consider the following lists to understand the below operations better.
L1 = [1, 2, 6, "Python"]
L2 = [9.1, 6, 7, "FACE Prep"]
Operator Description Example * List elements get repeated multiple number of times. + Concatenates two lists. Meaning, it appends their elements. in, not in Returns True if an element exists in the list and False otherwise. Function Description cmp (list1, list2) It compares the elements of both the lists. len (list) It is used to calculate the length of the list. max (list) It returns the maximum element of the list. min (list) It returns the minimum element of the list.
Some of the important Python List methods are given below. These help perform a lot of operations with ease.
Method Description pop() Removes and returns an element at the given index clear () Remove all the elements in a list count () Returns the index of the element or the number of times an element is present in the list reverse () Reverses the elements in a list