Element-wise complex test

Element-wise complex test

EasyNumPy Programs · 5 of 100

Write a NumPy program to test element-wise for complex numbers.

Input
[2, 4, 6+9j, 7]
Output
[False False  True False]

Approach

  1. np.iscomplex() is True where the imaginary part is non-zero.

Solution

Python
import numpy as npprint(np.iscomplex(np.array([2, 4, 6 + 9j, 7])))
Output
[False False  True False]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Element-wise NaN test

Element-wise NaN test

EasyNumPy Programs · 4 of 100

Write a NumPy program to test element-wise for NaN.

Input
[2, 4, np.nan, 7]
Output
[False False  True False] [False False False False]

Approach

  1. np.isnan(); x == np.nan never works because NaN is not equal to itself.

Solution

Python
import numpy as npa = np.array([2, 4, np.nan, 7])print(np.isnan(a), a == np.nan)
Output
[False False  True False] [False False False False]

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Element-wise finite test

Element-wise finite test

EasyNumPy Programs · 3 of 100

Write a NumPy program to test element-wise whether values are finite.

Input
[6, 8, 3, np.inf]
Output
[ True  True  True False]

Approach

  1. np.isfinite().

Solution

Python
import numpy as npprint(np.isfinite(np.array([6, 8, 3, np.inf])))
Output
[ True  True  True False]
Note

The original sample output was inverted; isfinite is True for normal numbers and False for infinity.

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Test that no element is zero

Test that no element is zero

EasyNumPy Programs · 2 of 100

Write a NumPy program to test whether none of the elements of an array is zero.

Input
[6, 8, 3, 5]
Output
True False

Approach

  1. np.all(arr) is True when every element is non-zero.

Solution

Python
import numpy as npprint(np.all(np.array([6, 8, 3, 5])), np.all(np.array([6, 0, 3])))
Output
True False

Line by line

Line 2

Zero counts as False, so a single 0 makes all False.

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Get the NumPy version

Get the NumPy version

EasyNumPy Programs · 1 of 100

Write a Python program to get the installed NumPy version.

Output
2.5.4

Approach

  1. np.__version__.

Solution

Python
import numpy as npprint(np.__version__)
Output
2.5.4

Line by line

Line 1

np is the standard alias for NumPy.

Learn the concept

New to this topic? Read the lesson, then come back.

More numpy programs

← All 100 NumPy Programs

Sum of unique values across lists

Sum of unique values across lists

EasyDictionary Programs · 94 of 94

Write a Python program to add up the distinct numbers found in all list values.

Input
{"a": [6, 7, 2, 8, 1], "b": [2, 3, 1, 6, 8, 10], "d": [1, 8, 2, 6, 9]}
Output
[1, 2, 3, 6, 7, 8, 9, 10] 46

Approach

  1. Union of all values in a set, then sum.

Solution

Python
d = {"a": [6, 7, 2, 8, 1], "b": [2, 3, 1, 6, 8, 10], "d": [1, 8, 2, 6, 9]}unique = set().union(*d.values())print(sorted(unique), sum(unique))
Output
[1, 2, 3, 6, 7, 8, 9, 10] 46

Line by line

Line 2

set().union(*lists) merges all the lists into one set of distinct numbers.

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs

Reverse values and decorate keys

Reverse values and decorate keys

EasyDictionary Programs · 93 of 94

Write a Python program to reverse each string value and put underscores around each key.

Input
{"a": "Python", "b": "Programming", "c": "Learning"}
Output
{'_a_': 'nohtyP', '_b_': 'gnimmargorP', '_c_': 'gninraeL'}

Approach

  1. Dict comprehension with an f-string key and [::-1] value.

Solution

Python
d = {"a": "Python", "b": "Programming", "c": "Learning"}print({f"_{k}_": v[::-1] for k, v in d.items()})
Output
{'_a_': 'nohtyP', '_b_': 'gnimmargorP', '_c_': 'gninraeL'}

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs

Invert a dictionary with repeated values

Invert a dictionary with repeated values

EasyDictionary Programs · 90 of 94

Write a Python program to invert a dictionary whose values repeat, collecting keys in lists.

Input
{"alex": 1, "bob": 2, "martin": 1, "robert": 2}
Output
{1: ['alex', 'martin'], 2: ['bob', 'robert']}

Approach

  1. setdefault(v, []).append(k).

Solution

Python
d = {"alex": 1, "bob": 2, "martin": 1, "robert": 2}inverted = {}for k, v in d.items():    inverted.setdefault(v, []).append(k)print(inverted)
Output
{1: ['alex', 'martin'], 2: ['bob', 'robert']}

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs

Rename a key

Rename a key

EasyDictionary Programs · 89 of 94

Write a Python program to rename the key “d” to “e”.

Input
{"a": 19, "b": 20, "c": 21, "d": 20}
Output
{'a': 19, 'b': 20, 'c': 21, 'e': 20}

Approach

  1. d["e"] = d.pop("d").

Solution

Python
d = {"a": 19, "b": 20, "c": 21, "d": 20}d["e"] = d.pop("d")print(d)
Output
{'a': 19, 'b': 20, 'c': 21, 'e': 20}

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs

Delete several keys

Delete several keys

EasyDictionary Programs · 88 of 94

Write a Python program to delete a list of keys from a dictionary.

Input
{"a": 19, "b": 20, "c": 21, "d": 20, "e": 50}, keys ["a", "d", "e"]
Output
{'b': 20, 'c': 21}

Approach

  1. pop(k, None) for each key.

Solution

Python
d = {"a": 19, "b": 20, "c": 21, "d": 20, "e": 50}for k in ["a", "d", "e"]:    d.pop(k, None)print(d)
Output
{'b': 20, 'c': 21}

Learn the concept

New to this topic? Read the lesson, then come back.

More dictionary programs

← All 94 Dictionary Programs