List Methods
Python provides list methods that allow us to work with lists.
Let’s learn few among them
- append()
- extend()
- insert()
- pop()
- clear()
- remove()
- sort()
- index()
Append
list.append(value)
Adds an element to the end of the list.Code
PYTHON
Output
Extend
list_a.extend(list_b)
Adds all the elements of a sequence to the end of the list.Code
PYTHON
Output
Insert
list.insert(index,value)
Element is inserted to the list at specified index.Code
PYTHON
Output
Pop
list.pop()
Removes last element.Code
PYTHON
Output
Remove
list.remove(value)
Removes the first matching element from the list.Code
PYTHON
Output
Clear
list.clear()
Removes all the items from the list.Code
PYTHON
Output
Index
list.index(value)
Returns the index at the first occurrence of the specified value.Code
PYTHON
Output
Count
list.count(value)
Returns the number of elements with the specified value.Code
PYTHON
Output
Sort
list.sort()
Sorts the list.Code
PYTHON
Output
Sort & Sorted
sort()
Modifies the listCode
PYTHON
Output
sorted()
Creates a new sorted listCode
PYTHON