Array/List methods in Python
What are array/list methods in Python? Together with Iteros Academy, we will introduce array/list methods in Python.
What are array/list methods in Python? Together with Iteros Academy, we will introduce array/list methods in Python.
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.
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.
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() adds an element to the end of the list
fruits = ["apple", "banana", "cherry"] fruits.append("orange") print(fruits) #Output: ['apple', 'banana', 'cherry', 'orange']
clear() removes all elements from the list
fruits = ["apple", "banana", "cherry"] fruits.clear() print(fruits) #Output: []
copy() returns a copy of the list
fruits = ["apple", "banana", "cherry"] x = fruits.copy() print(x) #Output: ['apple', 'banana', 'cherry']
count() returns the number of elements with the specified value
fruits = ["apple", "banana", "cherry"] x = fruits.count("cherry") print(x) #Output: 1
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() returns the index of the first element with the specified value
fruits = ['apple', 'banana', 'cherry'] x = fruits.index("cherry") print(x) #Output: 2
insert() adds an element at the specified position
fruits = ['apple', 'banana', 'cherry'] fruits.insert(1, "orange") print(fruits) #Output: ['apple', 'orange', 'banana', 'cherry']
pop() removes the element at the specified position
fruits = ['apple', 'banana', 'cherry'] fruits.pop(1) print(fruits) #Output: ['apple', 'cherry']
remove () removes the first item with the specified value
fruits = ['apple', 'banana', 'cherry'] fruits.remove("banana") print(fruits) #Output: ['apple', 'cherry']
reverse() reverses the order of the list
fruits = ['apple', 'banana', 'cherry'] fruits.reverse() print(fruits) #Output: ['cherry', 'banana', 'apple']
sort() sorts the list
cars = ['Ford', 'BMW', 'Volvo'] cars.sort() print(cars) #Output: ['BMW', 'Ford', 'Volvo']
Source » Itroz Academy
How to create an image with artificial intelligence (AI) has become a household word in recent years, with applications ranging from self-driving cars to
One of the most common and frustrating problems faced by WordPress users is the White Screen of Death (WSOD).
In this article, you will get to know the concept of inbound marketing, which is one of the most important things in website marketing. Be with Itroz Acade
To start a business on Instagram, you should pay attention to 8 key points. These 8 are the cornerstones of business on Instagram. Below is a description
Comments (0)