A list is a variable
type that can store multiple items
. This is mutable
, which means we can make changes to the list after creating it. This is also a type of Data Type.
Types of Lists:
Interception List Items:
Example - 1 :
It replaces the element at index 1 ("mother") with "daughter" using the assignment
obj[1] = "daughter"
.obj = ["father", "mother", "son"] obj[1] = "daughter" print(obj)
Example - 2 :
It will modify the main array by adding the second and third index that I provided.
obj = ["father", "mother", "son"] obj[1:2] = ["π", "π"] print(obj)
Example - 3 :
Accessing a list within a list means that the second list is inside the main list. To reach the second list, you first access the main variable and then navigate step by step to go deeper.
mixed_list = [1, "apple", 3.14, True, [5, 6, 7]] print(mixed_list) print(mixed_list[2]) print(mixed_list[4][1])
Examples of List Methods:
1. append ()
This file method adds a single element to the
end of the list
. We cannot directly append multiple elements in a singleappend()
call.obj = ["π", "π€ͺ", "π"] obj.append('πΊ') print('\nAppend String --', obj) obj = ["π", "π€ͺ", "π"] obj.append(True) print('\nAppend Boolean --', obj) obj = ["π", "π€ͺ", "π"] obj.append(4) print('\nAppend Number --', obj) obj = ["π", "π€ͺ", "π"] obj.append([4, 5]) print('\nAppend 2 elements in one list will convert a single emement --', obj)
2. extend ()
The
extend()
method adds the given list elements to the end of the current list. When you useextend()
with a string, each letter of the string is added to the list as a separate element.obj = ["π", "π€ͺ", "π"] obj.extend('πΊPritam') print('\nAppend String --', obj) obj = ["π", "π€ͺ", "π"] obj.extend([True]) print('\nAppend Boolean --', obj) obj = [12, 22, 33] obj.extend([1, 2, 3]) print('\nAppend List --', obj) obj = ['πΊ', 'π‘', 'π₯΅'] obj.extend(('β', 'π€οΈ', 'π§οΈ')) print('\nAppend Tuple --', obj)
3. insert ()
To add an item to a specific position in a list, you can use the
insert()
method. This method requires two arguments: one for the position where you want to add and one for the element.obj = ["π", "π€ͺ", "π"] obj.insert(1, 'πΊpritam') print('\nInsert emoji --', obj) obj = ["π", "π€ͺ", "π"] obj.insert(1, True) print('\nInsert Boolean --', obj) obj = [1, 2, 3] obj.insert(1, 4) print('\nInsert Number --', obj) obj = ["π", "π€ͺ", "π"] obj.insert(1, [4, 3]) print('\nInsert Number --', obj)
4. pop ()
The
pop()
method removes the specified index. If you don't mention the index, it removes thelast
item.obj = ["First", "Second", "Third"] obj.pop(2) print('\nRemove Last 2nd element --', obj) obj = [1, 2, 3] obj.pop() print('\nRemove last Item --', obj)
5. remove ()
The
remove()
method deletes the item we specify. We need to provide the exact item we want to remove.obj = ["First", "Second", "Third"] obj.remove("Second") print('\nRemove Last 2nd element --', obj) obj = [1, 2, 3] obj.remove(3) print('\nRemove last Item --', obj)
6. clear () & del
The
clear()
remove all elements from the lists. The list is still there, but it has no items in it. Thedel
keyword can delete the list entirely.obj = ["First", "Second", "Third"] obj.clear() print('\nRemove all the elements --', obj) obj = [1, 2, 3] obj.clear() print('\nRemove all the elements --', obj) del obj print('\nRemove the list --')
7. sort ()
List objects have a
sort()
method that will sort the list alphabetically or numerically, by default, in an ascending order.obj = ["C", "B", "A"] obj.sort() print('\nSort alphabetically --', obj) obj = ["C", "B", "a"] obj.sort() print('\nAscending Order by default --', obj) obj = ["C", "B", "a"] obj.sort(reverse=True) print('\nAscending Order by Manually --', obj) obj = [100, 2, 3] obj.sort() print('\nSort by Numerically --', obj)
8. copy () & list()
There are ways to duplicate a list. One method is to use the built-in List method
copy()
. Another way is to use the built-in methodlist()
.obj = ["First", "Second", "Third"] my_list = obj.copy() print('\nCopy main list --', my_list) obj = [1, 2, 3] my_list = list(obj) print('\nRemove all the elements --', obj)