List


append()

The append() method adds an item to the end of the list.
The append() method adds a single item to the existing list. It doesn't return a new list; rather it modifies the original list.
 
The syntax of append() method is:
list.append(item)

Example:- 
l = [1,2,3]
l.append(4)
a.append([5,6,7])
print(l)


Output:-
[1,2,3,4,[5,6,7]]

clear()

The clear() method removes all items from the list.

The syntax of clear() method is:
list.clear()

Example:-
list = [{1, 2}, ('a'), ['1.1', '2.2']]
list.clear()
print('List:', list)

Output:-
List: []

count()

The count() method returns the number of occurrences of an element in a list.
 
The syntax of count() method is:
list.count(element)

Example:-

vowels = ['a', 'e', 'i', 'o', 'i', 'u']
count = vowels.count('i')
print('The count of i is:', count)
count = vowels.count('p')
print('The count of p is:', count)

Output:-

The count of i is: 2
The count of p is: 0

extend()

The extend() extends the list by adding all items of a list (passed as an argument) to the end.

The syntax of extend() method is:
list1.extend(list2)

Example:-
l = [1,2,3]
l.extend([5,6,6])
print(l)

Output:-
[1,2,3,5,6,6]

index()

The index() method searches an element in the list and returns its index.
However, if the same element is present more than once, index() method returns its smallest/first position.
 
Note: Index in Python starts from 0 not 1.
 
The syntax of index() method for list is:
list.index(element)

Example:-

vowels = ['a', 'e', 'i', 'o', 'i', 'u']
index = vowels.index('e')
print(index)

index = vowels.index('ep')
print(index)

Output:-

1
ValueError: 'p' is not in list

insert()

The insert() method inserts an element to the list at a given index.

The syntax of insert() method is
  1. list.insert(index, element)
  2. insert() Parameters

The insert() function takes two parameters:
index - position where an element needs to be inserted
element - this is the element to be inserted in the list

Example:-
vowel = ['a', 'e', 'i', 'u']
vowel.insert(3, 'o')
print('Updated List: ', vowel)

Output:-
Updated List:  ['a', 'e', 'i', 'o', 'u']

remove()

The remove() method searches for the given element in the list and removes the first matching element.

The syntax of remove() method is:
list.remove(element)

Example:-
l = [1,2,3,4]
l.remove(4)
print(l)

Ouput:-
[1,2,3]

reverse()

The reverse() method reverses the elements of a given list.

The syntax of reverse() method is:
list.reverse()

Example:-
os = ['Windows', 'macOS', 'Linux']
print('Original List:', os)
os.reverse()
print('Updated List:', os)

Output:-
Original List: ['Windows', 'macOS', 'Linux']
Updated List: ['Linux', 'macOS', 'Windows']

sort()

The sort() method sorts the elements of a given list.
The sort() method sorts the elements of a given list in a specific order - Ascending or Descending.
 
The syntax of sort() method is:
list.sort(key=..., reverse=...)
sorted(list, key=..., reverse=...)
 
sort() Parameters
reverse - If true, the sorted list is reversed (or sorted in Descending order)
key - function that serves as a key for the sort comparison

Example:-
vowels = ['e', 'a', 'u', 'o', 'i']
vowels.sort()
print('Sorted list:', vowels)
vowels.sort(reverse=True)
print('Sorted list (in Descending):', vowels)

Output:-
Sorted list: ['a', 'e', 'i', 'o', 'u']
Sorted list (in Descending): ['u', 'o', 'i', 'e', 'a']