Array/List methods in Python

Array/List methods in Python

What are array/list methods in Python? Together with Iteros Academy, we will introduce array/list methods in Python.


If you have any questions, ask!

What are array/list methods in Python? Together with Iteros Academy, we will introduce array/list methods in Python. In the article about functions in Python, we examined the general and widely used methods of Python.

Array/List methods in Python

Arrays are used to store multiple values ​​in a single variable. An array is a special variable, which can hold more than one value at a time.

Arrays are very useful in website design .

In the following example, we have defined an array in Python that contains the names of three cars.

cars = ["Ford", "Volvo", "BMW"]
print(cars)

The variable above is equivalent to the variable below

car1 = "Ford"
car2 = "Volvo"
car3 = "BMW"

But with the difference that the names of three cars are placed in the array through a variable, but in the example above, three cars are defined in three variables. Many times we need to have a list of something and in that case arrays are perfect for us.

Accessing array elements

The list of each array has a number called an index. The array index counts from zero.

cars = ["Ford", "Volvo", "BMW"]

For example, ford is index zero of the array and volvo is index one.

In the above array there are names of three cars, but the array has two indices. Because indices start from zero.

Therefore, to access the elements of each array, we write its index number.

cars = ["Ford", "Volvo", "BMW"]
x = cars[1]

print(x)

By writing index 1, the name volvo is printed.

Now that you are familiar with the definition of arrays in Python, we study the methods that are useful for them.

 

append ()

append() adds an element to the end of the list

fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
print(fruits)

#Output: ['apple', 'banana', 'cherry', 'orange']

clear ()

clear() removes all elements from the list

fruits = ["apple", "banana", "cherry"]
fruits.clear()
print(fruits)

#Output: []

copy ()

copy() returns a copy of the list

fruits = ["apple", "banana", "cherry"]
x = fruits.copy()
print(x)

#Output: ['apple', 'banana', 'cherry']

count ()

count() returns the number of elements with the specified value

fruits = ["apple", "banana", "cherry"]
x = fruits.count("cherry")
print(x)

#Output: 1

extend()

extend() Add the elements of the list (or any iterable) to the end of the current list

fruits = ['apple', 'banana', 'cherry']
cars = ['Ford', 'BMW', 'Volvo']
fruits.extend(cars)
print(fruits)

#Output: ['apple', 'banana', 'cherry', 'Ford', 'BMW', 'Volvo']

index ()

index() returns the index of the first element with the specified value

fruits = ['apple', 'banana', 'cherry']
x = fruits.index("cherry")
print(x)

#Output: 2

insert ()

insert() adds an element at the specified position

fruits = ['apple', 'banana', 'cherry']
fruits.insert(1, "orange")
print(fruits)

#Output: ['apple', 'orange', 'banana', 'cherry']

pop ()

pop() removes the element at the specified position

fruits = ['apple', 'banana', 'cherry']
fruits.pop(1)
print(fruits)

#Output: ['apple', 'cherry']

remove ()

remove () removes the first item with the specified value

fruits = ['apple', 'banana', 'cherry']
fruits.remove("banana")
print(fruits)

#Output: ['apple', 'cherry']

reverse ()

reverse() reverses the order of the list

fruits = ['apple', 'banana', 'cherry']
fruits.reverse()
print(fruits)

#Output: ['cherry', 'banana', 'apple']

sort()

sort() sorts the list

cars = ['Ford', 'BMW', 'Volvo']
cars.sort()
print(cars)

#Output: ['BMW', 'Ford', 'Volvo']

Source » Itroz Academy

Related articles


Array/List methods in Python
Functions in Python
Class definition in Python
Basic concepts of introductory Python programming

Comments (0)

You need to login to post a comment.