List Methods in Python
-BY
24B81A05MB
Introduction to Lists:
1.Lists are sequence data type
2.Lists are ordered
3.Mutable collections of items in Python.
4.Can store elements of different data types.
5.Defined using square brackets: e.g., my_list = [1, 2, 'apple', 4.5]
Common List Methods:
• append() – Add an element to the end of the list
my_list = [10, 20, 30]
my_l ist.append(40) # [10, 20, 30, 40]
• insert() – Insert element at a specific index
my_list.insert(1, 15) # [10, 15, 20, 30, 40]
• remove() – Remove first matching element
my_list.remove(20) # [10, 15, 30, 40]
• pop() – Remove element at index (default last)
my_list.pop() # [10, 15, 30]
sort() – Sort the list
my_list.sort() # [10, 15, 30]
reverse() – Reverse the list order
my_list.reverse() # [30, 15, 10]
index()-Returns the position of the first occurrence of a specified value.
count():
Returns the number of times a value appears in the list.
extend():
Adds elements from another iterable (like a list) to the end of the current list.
TQ