Advanced Methods for Lists

Apart from basic list operations, certain Python methods can be applied to List objects. Some of them are used as given below.

1. list.extend()

Adds another list at the end of the list and the list got extended

Example

				
					list1 = ["orange", "apple", "banana", "apple"]

print("the original list is ")

print(list1)

Mylist  =  ["cherry", "pear"]

list1.extend(Mylist)

print("list after extend(Mylist):")

print(list1)
				
			
				
					the original list is 

['orange', 'apple', 'banana', 'apple']

list after extend(Mylist):

['orange', 'apple', 'banana', 'apple', 'cherry', 'pear']
				
			

2. list.insert()

Inserts the element at the desired position. Two arguments are to be given, one is the index position, and the other one is the value to be inserted.

 Example

				
					list1 = ["orange", "apple", "banana", "apple"]

print("the original list is ")

print(list1)

list1.insert(3, "NEW")

print("list after insert(3, \"NEW\") ")

print(list1)
				
			
				
					the original list is 

['orange', 'apple', 'banana', 'apple']

list after insert(3, "NEW") 

['orange', 'apple', 'banana', 'NEW', 'apple']
				
			

3. list.remove()

Removes the first occurrence of a particular item from the list given as the argument. If there are similar strings, then the first occurrence will be removed keeping the remaining.

Example

				
					list1 = ["orange", "apple", "banana", "apple"]

print ("the original list is:")

print(list1)

list1.remove("apple")

print ("the list after list1.remove(\"apple\"):")

print(list1)
				
			
				
					the original list is:

['orange', 'apple', 'banana', 'apple']

the list after list1.remove("apple"):

['orange', 'banana', 'apple']
				
			

4. list.clear()

Deletes all the items from the given list.

Example

				
					list1 = ["orange", "apple", "banana", "apple"]

print("the original list is: ")

print(list1)

list1.clear()

print("list after list1.clear()")

print(list1)
				
			
				
					the original list is: 

['orange', 'apple', 'banana', 'apple']

list after list1.clear()

[]
				
			

5. list.index()

Returns the index position of the given argument if it is present, 0 otherwise.

Example

				
					list1 = ["orange", "apple", "banana", "apple"]

print("the original list is:")

print(list1)

x = list1.index("banana")

print("the index position is", x)
				
			
				
					the original list is:

['orange', 'apple', 'banana', 'apple']

the index position is 2
				
			

6. list.count()

Returns the number of times an item occurs in the given list, returns 0 otherwise.

Example

				
					list1 = ["orange", "apple", "banana", "apple"]

print("the original list is:")

print(list1)

x = list1.count("apple")

print(f"the given string appears {x} times")
				
			
				
					the original list is:

['orange', 'apple', 'banana', 'apple']

the given string appears 2 times
				
			

7. list.sort()

Returns the sorted list.

Example

				
					list1 = ["orange", "apple", "banana", "apple"]

print("the original list is:")

print(list1)

list1.sort()

print("list after list1.sort()")

print(list1)
				
			
				
					the original list is:

['orange', 'apple', 'banana', 'apple']

list after list1.sort()

['apple', 'apple', 'banana', 'orange']
				
			

8. list.copy()

Returns the copy of the list.

Example

				
					list1 = ["orange", "apple", "banana", "apple"]

print("the original list1 is: ")

print(list1)

x = list1.copy()

print("x (copy of list1): ")

print(x)
				
			
				
					the original list1 is: 

['orange', 'apple', 'banana', 'apple']

x (copy of list1): 

['orange', 'apple', 'banana', 'apple']
				
			

9. list.reverse()

Returns the list in the reverse order of elements.

Example

				
					list1 = ["orange", "apple", "banana"]

print("the original list1 is:")

print(list1)

list1.reverse()

print("list1 after list1.reverse()")

print(list1)
				
			
				
					the original list1 is:

['orange', 'apple', 'banana']

list1 after list1.reverse()

['banana', 'apple', 'orange']


				
			

Stack operations on Lists

list.append() and list.pop() are used for stack operations.

10. list.append()

This method adds a member to the end of the List. It is used for stack push operation.

Example

				
					list1 = ["orange", "apple", "banana"]

print("the original list1 is:")

print(list1)

list1.append("coconut")

print("list after list1.append(\"coconut\")")

print(list1)
				
			
				
					the original list1 is:

['orange', 'apple', 'banana']

list after list1.append("coconut")

['orange', 'apple', 'banana', 'coconut']
				
			

11. list.pop()

Performs stack operation as it pops out the specified element from the list. The default argument is -1, which pops out the topmost (index length-1) element.

Example

				
					list1 = ["orange","apple", "banana"]

print("the original list is: ")

print(list1)

list1.pop(1)

print("list after list1.pop(1):")

print(list1)
				
			
				
					the original list is: 

['orange', 'apple', 'banana']

list after list1.pop(1):

['orange', 'banana']


				
			

Queue operations on Lists

The deque object allows all the operations that can be performed on a doubly ended queue. Both addition and deletion can be performed from both ends. 

12. Append with dequeue

Example

				
					from collections import deque

list1 = ["orange", "apple"]

dq = deque(list1)

print('Original deque:')

print(dq) 

dq.append("terry")

print('The deque after append from right:')

print(dq)
				
			
				
					Original deque:

deque(['orange', 'apple'])

The deque after append from right:

deque(['orange', 'apple', 'terry'])

				
			

13. popleft()

Shifts the elements to the left dropping the leftmost element. The dropped element is added to the other end.

Example

				
					from collections import deque

list1 = ["orange", "apple"]

dq = deque(list1)

print('Original deque:')

print(dq) 

dq.popleft()

print('The deque after dropping leftmost item:')

print(dq)
				
			
				
					Original deque:

deque(['orange', 'apple'])

The deque after dropping leftmost item:

deque(['apple'])
				
			

14. List Comprehensions

List comprehension provides shortcuts for creating a new list from an existing list. Look at the following program. Two lists are being created.

Example

				
					list1 = [“apple”, “banana”, “mango”]

newlist = []

for x in list1:

   If “n” in x:

      newlist.append(x)

print(newlist)
				
			
				
					

[ “banana”, “mango”]