Python Array Methods

Python Array Methods

Kishore V


Python Array Methods

Python lists (often used as arrays) come with many built-in methods that make it easy to manage, modify, and analyze data stored in them.

Below is a rewritten explanation of common array methods, along with updated and original examples.

Common Array Methods

append() – Add an Item to the End

Adds a single element at the end of the list.

Output:

['red', 'blue', 'green', 'yellow']
Try it Yourself

clear() – Remove All Elements

Deletes all items from the list, making it empty.

Output:

[]
Try it Yourself

copy() – Create a Shallow Copy

Returns a new list with the same elements.

Output:

['pen', 'pencil', 'eraser']
Try it Yourself

count() – Count Occurrences of a Value

Returns how many times a value appears in the list.

Output:

3
Try it Yourself

extend() – Add Multiple Elements

Adds elements from another iterable to the end of the list.

Output:

['HTML', 'CSS', 'JavaScript', 'Python']
Try it Yourself

index() – Find Position of an Element

Returns the index of the first occurrence of a value.

Output:

1
Try it Yourself

insert() – Add an Element at a Specific Position

Inserts an element at the given index.

Output:

['Delhi', 'Bengaluru', 'Mumbai', 'Chennai']
Try it Yourself

pop() – Remove an Element by Index

Removes and returns the element at the given position.

Output:

study
['exercise', 'sleep']
Try it Yourself

remove() – Remove an Element by Value

Removes the first matching value from the list.

Output:

['apple', 'cherry', 'banana']
Try it Yourself

reverse() – Reverse the List Order

Reverses the elements of the list in place.

Output:

[5, 4, 3, 2, 1]
Try it Yourself

sort() – Sort the List

Sorts the list in ascending order by default.

Output:

[60, 72, 83, 88, 95]
Try it Yourself

Sorting in descending order:

Output:

[95, 88, 83, 72, 60]
Try it Yourself

Summary of Array Methods

Method Purpose
append() Add item at the end
clear() Remove all elements
copy() Create a duplicate list
count() Count occurrences
extend() Add multiple items
index() Find element position
insert() Add item at specific index
pop() Remove item by index
remove() Remove item by value
reverse() Reverse list order
sort() Sort list elements

Our website uses cookies to enhance your experience. Learn More
Accept !