Removing Array Items in Python: Deleting Elements from Arrays

thumb_up 1  ·  sell Python array item removal, Deleting elements from Python arrays, Removing items from a Python array

The array class defines two methods with the help of which we can remove an element from the array. It has remove() and pop() methods

array.remove() Method

The remove() method removes the first occurrence of a given value from the array

Syntax

array.remove(v)

Parameters

  • v − The value to be removed from the array

Example

 
import array as arr a = arr.array('i', [1, 2, 1, 4, 2]) a.remove(2) print (a)

It will produce the following output −

array('i', [1, 1, 4, 2])

array.pop() Method

The pop() method removes an element at the specified index from the array, and returns the removed element.

Syntax

array.pop(i)

Parameters

  • i − The index for the eminent to be removed. The method returns element at ith position after removal.

Example

 
import array as arr a = arr.array('i', [1, 2, 1, 4, 2]) a.pop(2) print (a)

It will produce the following output −

array('i', [1, 2, 4, 2])



The End! should you have any inquiries, we encourage you to reach out to the Vercaa Support Center without hesitation.

Was this answer helpful?

Related Articles

description

Exploring Python's Key Characteristic

Python is a feature rich high-level, interpreted, interactive and object-oriented scripting language. This tutorial will list down some of…

arrow_forward
description

Comparing Python and C++

Both Python and C++ are among the most popular programming languages. Both of them have their advantages and disadvantages. In this…

arrow_forward
description

Creating a Python Hello World Program

This tutorial will teach you how to write a simple Hello World program using Python Programming language. This program will make use of…

arrow_forward
description

Python's Versatile Application Domains

Python is a general-purpose programming language. It is suitable for development of wide range of software applications. Over last few…

arrow_forward
description

Understanding the Python Interpreter

Python is an interpreter-based language. In a Linux system, Python's executable is installed in /usr/bin/ directory. For Windows, the…

arrow_forward
arrow_back « Back