A Quick Guide to Python List Methods with Examples

Usool - Sep 11 - - Dev Community

Introduction

Python lists are versatile and come with a variety of built-in methods that help in manipulating and processing data efficiently. Below is a quick reference to all the major list methods along with brief examples.

1. append(item)

Adds an item to the end of the list.

lst = [1, 2, 3]
lst.append(4)  # [1, 2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

2. clear()

Removes all items from the list.

lst = [1, 2, 3]
lst.clear()  # []
Enter fullscreen mode Exit fullscreen mode

3. copy()

Returns a shallow copy of the list.

lst = [1, 2, 3]
new_lst = lst.copy()  # [1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

4. count(item)

Counts the occurrences of an item.

lst = [1, 2, 2, 3]
lst.count(2)  # 2
Enter fullscreen mode Exit fullscreen mode

5. extend(iterable)

Extends the list by appending all elements from the iterable.

lst = [1, 2, 3]
lst.extend([4, 5])  # [1, 2, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

6. index(item, start, end)

Returns the index of the first occurrence of an item.

lst = [1, 2, 3]
lst.index(2)  # 1
Enter fullscreen mode Exit fullscreen mode

7. insert(index, item)

Inserts an item at the specified index.

lst = [1, 2, 3]
lst.insert(1, 'a')  # [1, 'a', 2, 3]
Enter fullscreen mode Exit fullscreen mode

8. pop(index)

Removes and returns the item at the specified index (default is the last item).

lst = [1, 2, 3]
lst.pop()  # 3, lst = [1, 2]
Enter fullscreen mode Exit fullscreen mode

9. remove(item)

Removes the first occurrence of an item.

lst = [1, 2, 3]
lst.remove(2)  # [1, 3]
Enter fullscreen mode Exit fullscreen mode

10. reverse()

Reverses the items in the list in place.

lst = [1, 2, 3]
lst.reverse()  # [3, 2, 1]
Enter fullscreen mode Exit fullscreen mode

11. sort(key, reverse)

Sorts the list in place (ascending by default).

lst = [3, 1, 2]
lst.sort()  # [1, 2, 3]
lst.sort(reverse=True)  # [3, 2, 1]
Enter fullscreen mode Exit fullscreen mode

12. sorted()

Returns a new sorted list from the items in an iterable.

lst = [3, 1, 2]
sorted(lst)  # [1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

13. len(list)

Returns the number of items in a list.

lst = [1, 2, 3]
len(lst)  # 3
Enter fullscreen mode Exit fullscreen mode

14. max(list)

Returns the largest item in a list.

lst = [1, 2, 3]
max(lst)  # 3
Enter fullscreen mode Exit fullscreen mode

15. min(list)

Returns the smallest item in a list.

lst = [1, 2, 3]
min(lst)  # 1
Enter fullscreen mode Exit fullscreen mode

16. sum(list)

Returns the sum of all items in a list.

lst = [1, 2, 3]
sum(lst)  # 6
Enter fullscreen mode Exit fullscreen mode

17. list()

Creates a list from an iterable.

s = "abc"
lst = list(s)  # ['a', 'b', 'c']
Enter fullscreen mode Exit fullscreen mode

Conclusion

These list methods cover the core functionalities you will need while working with lists in Python. Whether itโ€™s appending items, sorting, or making shallow copies, these methods allow you to manipulate data efficiently.

. . . . . . . . . . . . . . . . . . .
Terabox Video Player