Removing array elements in python

Python Arrays

Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.

Arrays

Note: This page shows you how to use LISTS as ARRAYS, however, to work with arrays in Python you will have to import a library, like the NumPy library.

Arrays are used to store multiple values in one single variable:

Example

Create an array containing car names:

What is an Array?

An array is a special variable, which can hold more than one value at a time.

If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?

An array can hold many values under a single name, and you can access the values by referring to an index number.

Access the Elements of an Array

You refer to an array element by referring to the index number.

Example

Get the value of the first array item:

Example

Modify the value of the first array item:

The Length of an Array

Use the len() method to return the length of an array (the number of elements in an array).

Example

Return the number of elements in the cars array:

Note: The length of an array is always one more than the highest array index.

Looping Array Elements

You can use the for in loop to loop through all the elements of an array.

Example

Print each item in the cars array:

Читайте также:  Run python script in java

Adding Array Elements

You can use the append() method to add an element to an array.

Example

Add one more element to the cars array:

Removing Array Elements

You can use the pop() method to remove an element from the array.

Example

Delete the second element of the cars array:

You can also use the remove() method to remove an element from the array.

Example

Delete the element that has the value «Volvo»:

Note: The list’s remove() method only removes the first occurrence of the specified value.

Array Methods

Python has a set of built-in methods that you can use on lists/arrays.

Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the specified value
extend() Add the elements of a list (or any iterable), to the end of the current list
index() Returns the index of the first element with the specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the first item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list

Note: Python does not have built-in support for Arrays, but Python Lists can be used instead.

Источник

Remove Element from an Array/List in Python

This tutorial will go through some common ways for removing elements from Python arrays/lists.

Arrays or Lists?

Python’s built-in sequence representation is a list, defined as a heterogenous sequence of elements, where each element has a definitive index in the sequence. To use arrays, you’d have to import the array module, which does ship with Python itself, but lists are far more commonly used.

Additionally — since the list syntax looks a lot like the syntax you’d use to define arrays in other programming languages — the terms «array» and «list» are oftentimes used interchangeably, even though they’re not the same data structure. It’s worth noting that many of these methods work both for an array and a list!

Читайте также:  Python else не работает

Using remove()

Appropriately, the remove() function can be used on any array or list in Python. To use it, we can simply pass the value of the element we want to remove. Let’s imagine we have the following array:

array = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] 

To remove, say, element 40 , we would simply write:

The result is the same array without the value 40 :

[10, 20, 30, 50, 60, 70, 80, 90, 100] 

Using pop()

The pop() function accepts the index of the element we want to remove. If we had the same array/list as before (with values from 10 to 100), we could write something like the following:

If we printed the result of the pop method, it would be the value 40 :

[10, 20, 30, 50, 60, 70, 80, 90, 100] 

Similarly to how pop() works in the stack data structure, here pop() also returns the value that it had just removed.

The only difference is that with arrays, we can remove an arbitrary element. With stacks, only the top element (i.e. the last one added) can ever be removed.

Using del

del is a python keyword used for deleting objects. Its exact behavior changes depending on the context, so we can also use it to remove list elements, though, arrays don’t support this. Once again, let’s take the same array and index as before:

array = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100] index = 3 

To remove the element at index 3 , we simply type the following:

If we now printed the contents of our array, we would get the following output:

[10, 20, 30, 50, 60, 70, 80, 90, 100] 

Using numpy Arrays

NumPy arrays are commonly used (especially in machine learning), so let’s show one of the ways to remove an element from a numpy array. Before using numpy , it is necessary to import it with:

To create a numpy array, we can wrap our current list using np.array() as such:

Читайте также:  Tuple python как работать

Alternatively, we could also declare a new array inside the method call itself:

a = np.array([10, 20, 30, 40, 50, 60, 70, 80, 90, 100]) 

Now to remove an element at index 3 , we use the following code:

Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

index = 3 a = np.delete(a, index) 

delete() is a static method declared in the numpy module. It accepts the array and the index of the element to remove.

The method returns a new array without the removed element:

[10, 20, 30, 50, 60, 70, 80, 90, 100] 

Conclusion

There are different ways to remove a list element in Python. Sometimes we might want to remove an element by index and sometimes by value. Sometimes we’re using Python’s default array and sometimes a numpy array.

In all these cases, it’s good to have multiple options to help us decide which of the techniques to use.

Источник

Python Remove Array Item

You can use the pop() method to remove an element from the array.

Example

Delete the second element of the cars array:

You can also use the remove() method to remove an element from the array.

Example

Delete the element that has the value «Volvo»:

Note: The list’s remove() method only removes the first occurrence of the specified value.

Unlock Full Access 50% off

COLOR PICKER

colorpicker

Join our Bootcamp!

Report Error

If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

Thank You For Helping Us!

Your message has been sent to W3Schools.

Top Tutorials
Top References
Top Examples
Get Certified

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Источник

Оцените статью