This set method removes an element but does not raise an exception if the element is not found

Before you can catch an exception, some code somewhere must throw one. Any code can throw an exception: your code, code from a package written by someone else such as the packages that come with the Java platform, or the Java runtime environment. Regardless of what throws the exception, it's always thrown with the throw statement.

As you have probably noticed, the Java platform provides numerous exception classes. All the classes are descendants of the Throwable class, and all allow programs to differentiate among the various types of exceptions that can occur during the execution of a program.

You can also create your own exception classes to represent problems that can occur within the classes you write. In fact, if you are a package developer, you might have to create your own set of exception classes to allow users to differentiate an error that can occur in your package from errors that occur in the Java platform or other packages.

You can also create chained exceptions. For more information, see the Chained Exceptions section.

The throw Statement

All methods use the throw statement to throw an exception. The throw statement requires a single argument: a throwable object. Throwable objects are instances of any subclass of the Throwable class. Here's an example of a throw statement.

throw someThrowableObject;

Let's look at the throw statement in context. The following pop method is taken from a class that implements a common stack object. The method removes the top element from the stack and returns the object.

public Object pop() { Object obj; if (size == 0) { throw new EmptyStackException(); } obj = objectAt(size - 1); setObjectAt(size - 1, null); size--; return obj; }

The pop method checks to see whether any elements are on the stack. If the stack is empty (its size is equal to 0), pop instantiates a new EmptyStackException object (a member of java.util) and throws it. The Creating Exception Classes section in this chapter explains how to create your own exception classes. For now, all you need to remember is that you can throw only objects that inherit from the java.lang.Throwable class.

Note that the declaration of the pop method does not contain a throws clause. EmptyStackException is not a checked exception, so pop is not required to state that it might occur.

Throwable Class and Its Subclasses

The objects that inherit from the Throwable class include direct descendants (objects that inherit directly from the Throwable class) and indirect descendants (objects that inherit from children or grandchildren of the Throwable class). The figure below illustrates the class hierarchy of the Throwable class and its most significant subclasses. As you can see, Throwable has two direct descendants: Error and Exception.

The Throwable class.

Error Class

When a dynamic linking failure or other hard failure in the Java virtual machine occurs, the virtual machine throws an Error. Simple programs typically do not catch or throw Errors.

Exception Class

Most programs throw and catch objects that derive from the Exception class. An Exception indicates that a problem occurred, but it is not a serious system problem. Most programs you write will throw and catch Exceptions as opposed to Errors.

The Java platform defines the many descendants of the Exception class. These descendants indicate various types of exceptions that can occur. For example, IllegalAccessException signals that a particular method could not be found, and NegativeArraySizeException indicates that a program attempted to create an array with a negative size.

One Exception subclass, RuntimeException, is reserved for exceptions that indicate incorrect use of an API. An example of a runtime exception is NullPointerException, which occurs when a method tries to access a member of an object through a null reference. The section Unchecked Exceptions — The Controversy discusses why most applications shouldn't throw runtime exceptions or subclass RuntimeException.

Removes an item from a list

Usage

Use remove() method to remove a single item from a list.

The method searches for the first instance of the given item and removes it. If specified item is not found, it raises ‘ValueError’ exception.

Syntax

list.remove(item)

Python list remove() method parameters
itemRequiredAny item you want to remove

# Remove 'green' L = ['red', 'green', 'blue'] L.remove('green') print(L) # Prints ['red', 'blue']

# Remove item from the nested list L = ['red', 'green', [1, 2, 3]] L.remove([1, 2, 3]) print(L) # Prints ['red', 'green']

The remove() method removes item based on specified value and not by index. If you want to delete list items based on the index, use pop() method or del keyword.

remove() method searches for the first instance of the given item and removes it.

L = ['red', 'green', 'blue', 'red', 'red'] L.remove('red') print(L) # Prints ['green', 'blue', 'red', 'red']

If you want to remove multiple instances of an item in a list, use list comprehension or lambda expression.

# list comprehension L = ['red', 'green', 'blue', 'red', 'red'] L = [x for x in L if x is not 'red'] print(L) # Prints ['green', 'blue'] # lambda expression L = ['red', 'green', 'blue', 'red', 'red'] L = list(filter(lambda x: x is not 'red', L)) print(L) # Prints ['green', 'blue']

Removing Item that Doesn’t Exist

remove() method raises an ValueError exception, if specified item doesn’t exist in a list.

L = ['red', 'green', 'blue'] L.remove('yellow') # Triggers ValueError: list.remove(x): x not in list

To avoid such exception, you can check if item exists in a list, using in operator inside if statement.

L = ['red', 'green', 'blue'] if 'yellow' in L: L.remove('yellow')

In this article, we will see how to remove an element in a set, using the discard() and remove() method. We will also learn the difference between the two methods, although both of them produce the same results.

Examples:



Input : set = ([10, 20, 26, 41, 54, 20]) Output : {41, 10, 26, 54} Input : set = (["ram", "aakash", "kaushik", "anand", "prashant"]) Output : {'ram', 'prashant', 'kaushik', 'anand'}

Method 1: Use of discard() method

The built-in method, discard() in Python, removes the element from the set only if the element is present in the set. If the element is not present in the set, then no error or exception is raised and the original set is printed.
If the element is present in the set:

def Remove(sets):

    sets.discard(20)

    print (sets)

sets = set([10, 20, 26, 41, 54, 20])

Remove(sets)

Output:

{41, 10, 26, 54}

If the element is not present in the set:

def Remove(sets):

    sets.discard(21)

    print (sets)

sets = set([10, 20, 26, 41, 54, 20])

Remove(sets)

Output:

{41, 10, 26, 20, 54}

Method 2: Use of remove() method

The built-in method, remove() in Python, removes the element from the set only if the element is present in the set, just as the discard() method does but If the element is not present in the set, then an error or exception is raised.
If the element is present in the set:

def Remove(sets):

    sets.remove("aakash")

    print (sets)

sets = set(["ram", "aakash", "kaushik", "anand", "prashant"])

Remove(sets)

Output:

{'ram', 'anand', 'prashant', 'kaushik'}

If the element is not present in the set:

def Remove(sets):

    sets.remove("gaurav")

    print (sets)

sets = set(["ram", "aakash", "kaushik", "anand", "prashant"])

Remove(sets)

Output:

No Output

Error:

Traceback (most recent call last): File "/home/bf95b32da22ada77d72062a73d3e0980.py", line 9, in Remove(sets) File "/home/bf95b32da22ada77d72062a73d3e0980.py", line 4, in Remove sets.remove("gaurav") KeyError: 'gaurav'

Article Tags :

Última postagem

Tag