Python List Methods https://www.skillvertex.com/blog Thu, 11 Apr 2024 12:03:23 +0000 en-US hourly 1 https://wordpress.org/?v=6.6.1 https://www.skillvertex.com/blog/wp-content/uploads/2024/01/favicon.png Python List Methods https://www.skillvertex.com/blog 32 32 Python List Methods https://www.skillvertex.com/blog/python-list-methods/ https://www.skillvertex.com/blog/python-list-methods/#respond Thu, 11 Apr 2024 12:03:23 +0000 https://www.skillvertex.com/blog/?p=8139 Read more]]>

Table of Contents

The Python List Method will allow you to built-in methods and will do operations on the Python list/ arrays. Read this article to learn more about Python list Methods.

What is Python List Methods?

Python consists of a set of built-in methods that will be used on lists.

S.noMethodDescription
1append()append() will work to add elements to the end of the List. 
2copy()Copy() will return a shallow copy of a list
3clear()clear() method is used for removing all items from the list. 
4count()count() method will count the elements.
5extend()extend() is used to add each element of an iterable to the end of the List.
6index()index() will return the lowest index where the element appears. 
7insert()This method will insert a given element at a given index in a list. 
8pop()This will remove and return the last value from the List or the given index value.
9remove()It will remove the given object from the List. 
10reverse()It will reverse objects of the List in place.
11sort()Sort() will sort the List in ascending, descending, or user-defined order.
12min()min() will calculate the minimum of all the elements of the List
13max()max will calculates the maximum of all the elements of the List

How to add Elements to the List?

The built-in Python methods are used to add the element to the list.

1. Python append() method

This method is used to add the element to the end of the list.

Syntax: list.append (element)

Example

class CustomList:
    def __init__(self):
        self.data = []

    def append(self, value):
        self.data += [value]

    def __str__(self):
        return str(self.data)


# Example usage
custom_list = CustomList()
custom_list.append(1)
custom_list.append(2)
custom_list.append(3)

print("Custom list after appending elements:", custom_list)

Output

Custom list after appending elements: [1, 2, 3]

2. Python insert() method

The Python insert() method will allow you to insert the element at the specified position.

Syntax:

list.insert(<position, element)

Example

class CustomList:
    def __init__(self):
        self.data = []

    def insert(self, index, value):
        self.data = self.data[:index] + [value] + self.data[index:]

    def __str__(self):
        return str(self.data)


# Example usage
custom_list = CustomList()
custom_list.insert(0, 1)
custom_list.insert(1, 2)
custom_list.insert(1, 3)

print("Custom list after inserting elements:", custom_list)

Output

Custom list after inserting elements: [1, 3, 2]

3. Python extend() method

The Python extend method will add the items of the iterable to the end of the list.

Syntax: List1.extend(List2)

Example

class CustomList:
    def __init__(self):
        self.data = []

    def extend(self, iterable):
        self.data += iterable

    def __str__(self):
        return str(self.data)


# Example usage
custom_list = CustomList()
custom_list.extend([1, 2, 3])
custom_list.extend([4, 5, 6])

print("Custom list after extending elements:", custom_list)

Output

Custom list after extending elements: [1, 2, 3, 4, 5, 6]

What are the important functions of the Python List?

Some of the important functions of the Python list are given below:

1. Python sum() method

The Python sum method will allow you to calculate the sum of the elements of the list.

Syntax: sum(List)

Example

class CustomList:
    def __init__(self):
        self.data = []

    def append(self, value):
        self.data += [value]

    def sum(self):
        return sum(self.data)

    def __str__(self):
        return str(self.data)


# Example usage
custom_list = CustomList()
custom_list.append(1)
custom_list.append(2)
custom_list.append(3)

print("Custom list:", custom_list)
print("Sum of elements in custom list:", custom_list.sum())

Output

Custom list: [1, 2, 3]
Sum of elements in custom list: 6

2. Python count() method

This method will sum the total occurrence of the given element of the list.

Syntax: List.count(element)

Example

class CustomList:
    def __init__(self):
        self.data = []

    def append(self, value):
        self.data.append(value)

    def count(self, value):
        return self.data.count(value)

    def __str__(self):
        return str(self.data)


# Example usage
custom_list = CustomList()
custom_list.append(1)
custom_list.append(2)
custom_list.append(3)
custom_list.append(1)  # Adding one more 1

print("Custom list:", custom_list)
print("Number of occurrences of '1' in custom list:", custom_list.count(1))

Output

Custom list: [1, 2, 3, 1]
Number of occurrences of '1' in custom list: 2

3.Python index() method

This method will return the index of the first occurrence. So, the start and end indexes are not the required parameters.

Syntax: List.index(element[,start[,end]])

Example

class CustomList:
    def __init__(self):
        self.data = []

    def append(self, value):
        self.data.append(value)

    def index(self, value):
        return self.data.index(value)

    def __str__(self):
        return str(self.data)


# Example usage
custom_list = CustomList()
custom_list.append(1)
custom_list.append(2)
custom_list.append(3)

print("Custom list:", custom_list)
print("Index of '2' in custom list:", custom_list.index(2))

Output

Custom list: [1, 2, 3]
Index of '2' in custom list: 1

4. Python min() method

This method will find the minimum of all the elements of the list.

Syntax: min(iterable, *iterables[, key])

Example

class CustomList:
    def __init__(self):
        self.data = []

    def append(self, value):
        self.data.append(value)

    def min(self):
        return min(self.data)

    def __str__(self):
        return str(self.data)


# Example usage
custom_list = CustomList()
custom_list.append(3)
custom_list.append(1)
custom_list.append(5)
custom_list.append(2)

print("Custom list:", custom_list)
print("Minimum value in custom list:", custom_list.min())

Output

Custom list: [3, 1, 5, 2]
Minimum value in custom list: 1

5. Python max() method

The Python will calculate the maximum of all the elements of the list.

Syntax: max(iterable, *iterables[, key])

Example

class CustomList:
    def __init__(self):
        self.data = []

    def append(self, value):
        self.data.append(value)

    def max(self):
        return max(self.data)

    def __str__(self):
        return str(self.data)


# Example usage
custom_list = CustomList()
custom_list.append(3)
custom_list.append(1)
custom_list.append(5)
custom_list.append(2)

print("Custom list:", custom_list)
print("Maximum value in custom list:", custom_list.max())

Output

Custom list: [3, 1, 5, 2]
Maximum value in custom list: 5

6. Python sort() method

This method will allow us to sort the given data structure in ascending order. Whereas, the key and reverse flag won’t be considered as the necessary parameter and the reverse_ flag will be set to false when nothing is passed through the sorted().

Syntax: list.sort([key,[Reverse_flag]])

Example

class CustomList:
    def __init__(self):
        self.data = []

    def append(self, value):
        self.data.append(value)

    def sort(self):
        self.data.sort()

    def __str__(self):
        return str(self.data)


# Example usage
custom_list = CustomList()
custom_list.append(3)
custom_list.append(1)
custom_list.append(5)
custom_list.append(2)

print("Custom list before sorting:", custom_list)
custom_list.sort()
print("Custom list after sorting:", custom_list)

Output

Custom list before sorting: [3, 1, 5, 2]
Custom list after sorting: [1, 2, 3, 5]

7. Python reverse() method

The reverse method will reverse the order of the list.

Syntax: list. reverse()

Example

class CustomList:
    def __init__(self):
        self.data = []

    def append(self, value):
        self.data.append(value)

    def reverse(self):
        self.data = self.data[::-1]

    def __str__(self):
        return str(self.data)


# Example usage
custom_list = CustomList()
custom_list.append(1)
custom_list.append(2)
custom_list.append(3)

print("Custom list before reversing:", custom_list)
custom_list.reverse()
print("Custom list after reversing:", custom_list)

Output

Custom list before reversing: [1, 2, 3]
Custom list after reversing: [3, 2, 1]

Conclusion

Python lists are fundamental data structures that enable the storage and manipulation of collections of elements. For beginners learning Python, grasping basic list methods is crucial. These methods offer essential functionalities for managing lists effectively.

However, the append() method will allow adding elements to the end of a list, while extend() allows adding multiple elements from another iterable. insert() permits inserting elements at specific positions. For removing elements, students can utilize remove() to delete the first occurrence of a value or pop() to remove and return an element at a given index.

Python List Methods-FAQs

Q1.How do I list available methods in Python?

Ans. using the dir() in Python is used to list all the methods of the class.

Q2.What are the Python methods?

Ans. The three methods of Python include the Instance Method, Class Method, and Static Method.

Q3.What is the use of list () method?

Ans.The list() function will convert the iterable such as a string, tuple, or dictionary into the list.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

]]>
https://www.skillvertex.com/blog/python-list-methods/feed/ 0