DEV Community

Ankit Kumar
Ankit Kumar

Posted on

List Methods in Python

An overview of string methods in Python

In this article, I will provide an overview of the most commonly used string methods in Python.

1. append()

Used for appending and adding elements to the end of the List.
example:-

my_list = [1, 2, 3]
my_list.append(4)
print(my_list)  # Output: [1, 2, 3, 4]

Enter fullscreen mode Exit fullscreen mode

2. copy()

It returns a shallow copy of a list
example:-

my_list = [1, 2, 3]
new_list = my_list.copy()
print(new_list)  # Output: [1, 2, 3]

Enter fullscreen mode Exit fullscreen mode

3. clear()

This method is used for removing all items from the list.
example:-

my_list = [1, 2, 3] 
my_list.clear() print(my_list) # Output: []
Enter fullscreen mode Exit fullscreen mode

4. count()

These methods count the elements
example:-

my_list = [1, 2, 3, 2, 4, 2]
count = my_list.count(2)
print(count)  # Output: 3

Enter fullscreen mode Exit fullscreen mode

5. extend()

Adds each element of the iterable to the end of the List
example:-

my_list = [1, 2, 3]
my_list.extend([4, 5, 6])
print(my_list)  # Output: [1, 2, 3, 4, 5, 6]

Enter fullscreen mode Exit fullscreen mode

6. index()

Returns the lowest index where the element appears.
example:-

my_list = [1, 2, 3, 2, 4, 2]
index = my_list.index(2)
print(index)  # Output: 1

Enter fullscreen mode Exit fullscreen mode

7. insert()

Inserts a given element at a given index in a list.
example:-

my_list = [1, 2, 3, 4, 5]
my_list.insert(2, 'hello')
print(my_list)  # Output: [1, 2, 'hello', 3, 4, 5]

print("My name is {}, and I'm {} years old.".format(name, age))
 Output: "My name is Ankit, and I'm 25 years old."
Enter fullscreen mode Exit fullscreen mode

8. pop()

Removes and returns the last value from the List or the given index value.
example:-

my_list = [1, 2, 3, 4, 5]
removed_element = my_list.pop(2)
print(removed_element)  # Output: 3
print(my_list)  # Output: [1, 2, 4, 5]

Enter fullscreen mode Exit fullscreen mode

9. remove()

Removes a given object from the List.
example:-

my_list = [1, 2, 3, 4, 5]
my_list.remove(3)
print(my_list)  # Output: [1, 2, 4, 5]

Enter fullscreen mode Exit fullscreen mode

10. sort()

Sort a List in ascending, descending, or user-defined order
default is ascending and for reverse write 'reverse=True.
example :-

my_list = [3, 1, 4, 2, 5]
my_list.sort()
print(my_list)  # Output: [1, 2, 3, 4, 5]
my_list.sort(reverse=True) 
print(my_list) # Output: [5, 4, 3, 2, 1]
Enter fullscreen mode Exit fullscreen mode

11. List as Stack

Stack is data structure which works on last in first out principle.
List can be used as a stack by using the append() method to add elements to the top of the stack and the pop()
example:-

stack = []
stack.append('A')
stack.append('B')
stack.append('C')
print(stack)  # Output: ['A', 'B', 'C']
top = stack.pop()
print(top)  # Output: 'C'
print(stack)  # Output: ['A', 'B']

Enter fullscreen mode Exit fullscreen mode

12. List as Queue

Queue is data structure which work on last in last out principal.
A list can be used as a queue by using the append() method to add elements to the end of the queue and the pop(0)
example:-

queue = []
queue.append('A')
queue.append('B')
queue.append('C')
print(queue)  # Output: ['A', 'B', 'C']
front = queue.pop(0)
print(front)  # Output: 'A'
print(queue)  # Output: ['B', 'C']
Enter fullscreen mode Exit fullscreen mode

Using the pop(0) method to remove elements from the beginning of a list can be inefficient for large lists, as it requires shifting all the remaining elements one position to the left. If you need to implement a more efficient queue, it's recommended to use the deque class from the collections module
example:-

from collections import deque

queue = deque()
queue.append('A')
queue.append('B')
queue.append('C')
print(queue)  # Output: deque(['A', 'B', 'C'])
front = queue.popleft()
print(front)  # Output: 'A'
print(queue)  # Output: deque(['B', 'C'])
Enter fullscreen mode Exit fullscreen mode

Top comments (0)