Python List Methods



1) Python List append() Method

The append() a method is used to add the new element at the end of the list.

Syntax

myList.append(item)

Example

myList1 = ['Python', 'HTML', 'CSS']
print(myList1)

myList1.append('Django')
print(myList1)

Output

['Python', 'HTML', 'CSS']
['Python', 'HTML', 'CSS', 'Django']

2) Python List clear() Method

The clear() a method is used to remove all items from the list.  This method does not return anything


Syntax

myList.clear()

Example

myList1 = ['Python', 'HTML', 'CSS']
myList1.clear()
print(myList1)

Output

[]

3) Python List copy() Method

The copy() a method is used to copies the elements of one list into another.

Syntax

myList.copy()

Example

myList = ['Python', 'HTML', 'CSS']
newList = myList.copy()
print(newList)

# Check List Both Lists are same
print(myList == newList)

Output

['Python', 'HTML', 'CSS']
True

4) Python List count() Method

The copy() a method is used to count the number of occurrences of a particular item in a list.


Syntax

myList.count(item)

Example

myList = ['Python', 'HTML', 'CSS', 'HTML']
print(myList.count('HTML'))

Output

2


5) Python List extend() Method

The extend() a method is used to join two lists.

This method does not return anything; it modifies the list in place.


Syntax

myList.extend(iterable)

Example 1

myList = ['Python', 'HTML', 'CSS', 'HTML']
myList.extend([1, 2, 3])

print(myList)

Output

['Python', 'HTML', 'CSS', 'HTML', 1, 2, 3]

Example 2

myList = ['Python', 'HTML', 'CSS', 'HTML']
myList.extend((1, 2, 3))

print(myList)

Output

['Python', 'HTML', 'CSS', 'HTML', 1, 2, 3]

Example 3

myList = ['Python', 'HTML', 'CSS', 'HTML']
myList.extend({1, 2, 3})

print(myList)

Output

['Python', 'HTML', 'CSS', 'HTML', 1, 2, 3]

6) Python List index() Method

The index() the method returns the lowest index of a given element. If a specified item is not found, it raises the ‘ValueError’ exception.

The optional arguments start and end limit the search to a particular subsequence of the list.


Syntax

myList.index(item,start,end)

Example

myList = ['Python', 'HTML', 'CSS', 'HTML']
print(myList.index('HTML'))

Output

1

index() on Duplicate Items

If the list has many occurrences of the specified item, the index() method returns the index of the first instance only.


Example

myList = ['Python', 'HTML', 'CSS', 'HTML', 'Python']
print(myList.index('Python'))

Output

0


Limit index() Search to Subsequence

If you want to search the list from the middle, specify the start parameter.

You can also specify where to stop the search with the end parameter.


Example 1

myList = ['Python', 'HTML', 'CSS', 'HTML', 'Python']
print(myList.index('Python', 2))

Output

4

 


Example 2 

myList = ['Python', 'HTML', 'CSS', 'HTML', 'Python']
print(myList.index('Python', 2, 5))

Output

4

7) Python List insert() Method

The insert() function is used to add new items into the list at a particular index.

This method does not return anything; it changes the list in place.


Syntax

myList.insert(index,item)

Example

myList = ['Python', 'HTML', 'CSS']
myList.insert(2, 'Angular')

print(myList)

Output

['Python', 'HTML', 'Angular', 'CSS']

An index greater than list length

When you define an index greater than the list length, you do not get any difference. Instead, the item is inserted at the end of the list.


Example

myList = ['Python', 'HTML', 'CSS']
myList.insert(6, 'Angular')

print(myList)

Output

['Python', 'HTML', 'CSS', 'Angular']


8) Python List pop() Method

The pop() method deletes the element of the given index. It deletes the last item if we do not pass the index.


Syntax

myList.pop(index)

The pop() method returns the value of the removed item.


Example

myList = ['Python', 'HTML', 'CSS']
myList.pop(1)

print(myList)

Output

['Python', 'CSS']

When we remove an item from the list using pop(), it removes it and returns its value.


Example

myList = ['Python', 'HTML', 'CSS']
item = myList.pop(1)

print(item)

Output

HTML

When we don’t specify the index on pop(), it assumes the parameter to be -1 and removes the last item.


Example

myList = ['Python', 'HTML', 'CSS']
item = myList.pop()

print(item)

Output

CSS

9) Python List remove() Method

The remove() method is used to delete or remove an item from the list. If a defined item is not found, it raises the ‘ValueError’ exception.


Syntax

myList.remove(item)

Example

myList = ['Python', 'HTML', 'CSS']
myList.remove('Python')

print(myList)

Output

['HTML', 'CSS']


Remove Duplicate Items

The remove() method searches for the first instance of the given item and removes it.


Example

myList = ['Python', 'HTML', 'CSS', 'HTML']
myList.remove('HTML')

print(myList)

Output

['Python', 'CSS', 'HTML']

If we want to remove multiple instances of an item in a list, use list comprehension or lambda expression.


Example

myList = ['HTML', 'Python', 'HTML', 'CSS', 'HTML']
newList = [x for x in myList if x is not 'HTML']

print(newList)

Output

['Python', 'CSS']

10) Python List reverse() Method

The reverse() method is reversed elements of the list.


Syntax

myList.reverse()

Example

myList = ['Python', 'HTML', 'CSS', 'Java']

myList.reverse()

print(myList)

Output

['Java', 'CSS', 'HTML', 'Python']

11) Python List sort() Method

The sort() method sorts the list. by using this method we can display the list items in ascending order or descending order.



Syntax

myList.sort(key,reverse)

Example

myList = ['Python', 'HTML', 'CSS', 'Java']

myList.sort()

print(myList)

Output

['CSS', 'HTML', 'Java', 'Python']

But, you cannot sort lists that have both numbers and strings in them, since Python doesn’t know how to compare these values.


Example

myList = ['Python', 1, 2, 'HTML', 'CSS', 'Java', 23]

myList.sort()

print(myList)

Output

Traceback (most recent call last):
    myList.sort()
TypeError: '<' not supported between instances of 'int' and 'str'

Sort List in Reverse Order

We can also sort the list in reverse order by using reverse = TRUE.


Example

myList = ['Python', 'HTML', 'CSS', 'Java']

myList.sort(reverse=True)

print(myList)

Output

['Python', 'Java', 'HTML', 'CSS']

Sort with Key

Use key parameters for more complicated custom sorting. A key parameter defines a function to be executed on each list item before executing comparisons.

For example, with a list of strings, specifying key=len (the built-in len() function) sorts the strings by length, from shortest to longest.


Example

myList = ['Python', 'HTML', 'CSS', 'Angular']

myList.sort(key=len)

print(myList)

Output

['CSS', 'HTML', 'Python', 'Angular']