Facebook Twitter Instagram
    Facebook Twitter Instagram Pinterest Vimeo
    Hand On CodeHand On Code
    Hand On CodeHand On Code
    Home»python»Python Lists Methods
    python

    Python Lists Methods

    March 23, 2023No Comments5 Mins Read
    Share
    Facebook Twitter LinkedIn Pinterest Email

    Definition of Python Lists Methods

    Python lists are data structures with a group of values enclosed in square brackets that can be manipulated to our convenience using a number of predefined methods in the python programming language. These operations range from adding values to lists to deleting or removing values, slicing out a particular value from lists, sorting or rearranging the values in lists, and performing mathematical operations on lists like minimum, maximum, and summat. We will learn about Python lists in this topic.

    methods.

    Types of Python Lists Methods

    Some of the methods or built-in capabilities that Python has for lists include:

    • Append
    • Insert
    • Extend
    • Numerical methods (sum, count, index, min, and max)
    • Length
    • Sort
    • Reverse
    • Pop
    • Delete/remove

    For a clear knowledge of these built-in methods, we’ll go over each category with examples.

    python.

    1. Append

    Python’s list append function enables us to supplement an existing element or value.

    list.

    Code: list.append (value)

    lis1=["Horse","Shoe","Turtle",179, 200, 544]
    lis1.append('snail')
    print(lis1)
    Results:

    Python Lists Methods

    This example shows how to create a list of integers and strings called lis1 and append a new string value to it. The output is the addition of the appended element to the list.

    list.

    2. Insert

    Python’s list insert feature enables us to add a value to a particular spot in the

    list.

    the List.insert(position,value) code

    lis1=["Horse","Shoe","Turtle",179, 200, 544]
    lis1.insert(3,'snail')
    print(lis1)
    Produces:

    Python Lists Methods

    In this example, we’ve utilized the lis1 to add a new string to the existing lis1 in the third position adjacent to the value “Turtle,” and the output is printed as a result.

    3. Extend

    By joining one or more lists into a new list containing an extended version of the provided two, we may use the Python list extends method.

    lists.

    the List.extend(List) code

    lis1=["Horse","Shoe","Turtle"]
    lis2=[179, 200, 544]
    lis1.extend(lis2)
    print(lis1)
    lis2.extend(lis1)
    print(lis2)
    Produces:

    Python Lists Methods

    The output, which is the combining of the two lists, results from whichever way that the two lists, lis1 & lis2, are extended.

    printed.

    4. Sum

    The in-list sum method can be used to calculate the sum of all elements in a list when dealing with numerical data.

    list.

    the sum(list) code

    lis2=[179, 200, 544]
    sum(lis2)
    Produces:

    Python Lists Methods

    5. Count

    Python’s count function provides us with the total number of instances of a particular variable throughout the

    list.

    the List.count(value) code

    lis=["Horse","Shoe","Turtle",179, 200, 544,"Shoe"]
    lis.count('Shoe')
    Produces:

    Python Lists Methods

    Since “Shoe” appears twice in the provided list, the count function determines the precise element, counts the number of times it appears, and returns the number of occurrences.

    output.

    6. Index

    The Python list index method makes it possible to determine an element’s index position in a list. It can be provided with the start and end parameters if we wanted to limit the element or value we are searching for inside the

    list.

    the List.index(value,([start,end]) code

    lis=["Horse","Shoe","Turtle",179, 200, 544,"Shoe"]
    print(lis.index("Turtle"))
    print(lis.index(179,2,5))
    Results:

    Python Lists Methods

    In this illustration, we discover the index values of two elements in a list that contains both number and textual values. The first print statement finds the index of the string “Turtle,” which is at position 2 in the list, and the second print statement finds the index value of the number 179. We have provided start and end index parameters from positions 2 to 5, and the output gives us the value 3—which is the position of that value 179. The compiler throws an error if we supply the start and end parameters outside the list’s perimeter.

    error.

    7. Min&Max

    We can determine the element with the lowest and highest values using the min and max techniques.

    list.

    min(List)

    the max(List) code

    lis=[179, 200, 544, 11, 18, 2000]
    print(min(lis))
    print(max(lis))
    Produces:

    Python Lists Methods

    The aforementioned illustration provides the least and greatest value that may be found in the list of numerical values.

    elements.

    8. Length

    The total length or total amount of characters in the list is provided by the length method in the Python list. It contains every string, number, character other than a digit, and space in between list items. Code:

    lis1=[179, 200, 544, 11, 18, 2000]
    lis2="Dostoevsky is a legendary writer"
    print(len(lis1))
    print(len(lis2))
    Results:

    Python Lists Methods

    In this illustration, we’ve declared two lists, one with numerical values and the other being a statement with only string values. The length function determines the number of elements in the first list, which is 6, and determines the overall length of the second list, which contains just string values and includes all alphabets and spaces as characters.

    32.

    9. Sort

    Python lists and tuples can both utilize the sort method, which arranges the list or tuple in ascending order. As a result, the sort function arranges the list’s contents in ascending

    order.

    the List.sort() code

    lis1=[179, 200, 544, 11, 18, 2000]
    print(sorted(lis1))
    Produces:

    0

    We have arranged the list in ascending order of the

    values.

    10. Reverse

    Reversing the order of the values in the list using the provided flag

    order.

    the List.sort(reverse_Flag=True) code

    lis1=[179, 200, 544, 11, 18, 2000]
    lis1.sort(reverse=True)
    lis1
    Produces:

    1

    When we set the reverse flag, the reverse sorting begins.

    true.

    11. Pop

    A specific element in the list can be extracted using the pop method in the Python list. The pop method always returns the final member in the list if the index parameter is omitted. Code:

    lis1=[179, 200, 544, 11, 18, 2000]
    print(lis1.pop())
    print(lis1.pop(2))
    Produces:

    2

    Since we haven’t stated the index option, the first print statement publishes the list’s final member. We have stated the index 2 in the second print statement, and the output displays the second position in the

    list.

    12. Delete/Remove

    This method deletes or removes a specified element from the list; when defined, the delete and remove methods both carry out the same action. Code:

    lis1=[179, 200, 544, 11, 18, 2000]
    del lis1[1]
    print(lis1)
    lis1.remove(11)
    print(lis1)
    Produces:

    3

    The given element at index 1 has been removed by the delete method, and the list’s particular value 11, which was also provided, has been removed by the remove method.

    Learn Python free Python Code Python Course Free download python coursefree Courses Download Python Language Python Lists Methods
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticlePython Identifiers
    Next Article Python Reduce

    Related Posts

    python

    Class method vs Static method in Python

    April 7, 2023
    python

    Python Program to Count the Number of Matching Characters in a Pair of String

    April 7, 2023
    python

    Coroutine in Python

    April 7, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    Facebook Twitter Instagram Pinterest
    © 2023 ThemeSphere. Designed by ThemeSphere.

    Type above and press Enter to search. Press Esc to cancel.