Whether you are a Python beginner😵 or a proficient Python Programmer/Developer🤩, one of the most common Data Structure that you will encounter is List📃
- List, in Python, is an ordered, mutable, heterogeneous, dynamic Datatype.
- They are ordered, so we can access individual elements through index.
- They are mutable, means that the list elements can be updated.
- A List can contain data of various datatypes like Integer, Float, Boolean, String etc.
- List can contain duplicate elements.
- Example of a list is
new_list = ["John", 2 , 2.5, True]
Simple✨💥......isn't it??!!!!!!😎🤩
Okay..Let's discuss some of the most common methods that we can use upon Lists.
-
append()
This method is used to add/append elements at the end of the list.
Syntax
list.append(element)
Example
num_list = ["John", 1, 2, 3, 4, True]
num_list.append(5)
print("Updated List: {}".format(num_list))
Output
Updated List: ['John', 1, 2, 3, 4, True, 5]
-
insert()
This method is used to insert/add element at a specified location/index.
Syntax
list.insert(index, element)
Example
num_list = ["John", 1, 2, 3, 4, True]
num_list.insert(1,500)
print("Updated List: {}".format(num_list)
Output
Updated List: ["John", 500, 1, 2, 3, 4, True]
500 is inserted at index 1.
-
count()
This method returns the number of occurrence of a particular element in a list.
Syntax
list.count(element)
Example
num_list = [1,2,3,5,4,5,6,5]
cnt = num_list.count(5)
print("Number of times 5 occur: {}".format(cnt))
Output
Number of times 5 occur: 3
-
sort()
This method sorts the list in ascending order by default. To sort in descending order, we have to use reverse flag within the sort method.
Syntax
list.sort(reverse = True|False, key = function_name)
Example
num_list = [1,2,3,5,4,5,6,5]
num_list.sort()
print("Sorted List in Ascending Order: {}".format(num_list))
num_list.sort(reverse = True)
print("Sorted List in Descending Order: {}".format(num_list))
Output
Sorted List in Ascending Order: [1, 2, 3, 4, 5, 5, 5, 6]
Sorted List in Descending Order: [6, 5, 5, 5, 4, 3, 2, 1]
-
reverse()
As the name implies, this method reverses a list.
Syntax
list.reverse()
Example
num_list = [1,2,3,5,4,5,6,5]
num_list.reverse()
print("Reversed List: {}".format(num_list))
Output
Reversed List: [5, 6, 5, 4, 5, 3, 2, 1]
-
remove()
This method removes a specified element from the list. If the specified element occurs more than once, then the first occurrence of the element is removed.
Syntax
list.remove(element)
Example
num_list = [1,2,3,5,4,5,6,5]
print("List before updating: {}".format(num_list))
num_list.remove(5)
print("Updated List: {}".format(num_list))
Output
List before updating: [1, 2, 3, 5, 4, 5, 6, 5]
Updated List: [1, 2, 3, 4, 5, 6, 5]
-
pop()
This method removes the element from the specified location/index.
Syntax
list.pop(index)
Example
num_list = [1,2,3,5,4,5,6,5]
print("List before updating: {}".format(num_list))
num_list.pop(5)
print("Updated List: {}".format(num_list))
Output
List before updating: [1, 2, 3, 5, 4, 5, 6, 5]
Updated List: [1, 2, 3, 5, 4, 6, 5]
-
index()
This method returns the index of a specified element. If the specified element occurs more than once, then index of the first occurrence of the element is returned.
Syntax
list.index(element)
Example
num_list = [1,2,3,5,4,5,6,5]
idx = num_list.index(5)
print("Index of 5: {}".format(idx))
Output
Index of 5: 3
-
extend()
This method appends/adds the elements of any iterable (List, Set, Tuple, etc.) to the end of the current list.
Syntax
list.extend(iterable)
Example
num_list = [100,200,300,400,500]
char_list = ["A", "B", "C", "D"]
num_list.extend(char_list)
print("Updated List: {}".format(num_list))
Output
Updated List: [100, 200, 300, 400, 500, 'A', 'B', 'C', 'D']
-
copy()
This method creates a copy of the specified list.
Syntax
list_2 = list_1.copy()
list_2 is the copy of list_1
Example
num_list = [100,200,300,400,500]
new_list = num_list.copy()
print("Copy of the 'num_list': {}".format(new_list))
Output
Copy of the 'num_list': [100, 200, 300, 400, 500]
-
clear()
This method removes all the elements from the specified list.
Syntax
list.clear()
Example
num_list = [100,200,300,400,500]
num_list.clear()
print("Cleared List: {}".format(num_list))
Output
Cleared List: []
So...working with a List isn't that difficult!!....right???!!!!😇
Happy Learning!!✌😎
Top comments (0)