Extract, replace, and transform specific elements of Python lists (arrays)

Money and Business

To generate a new list in Python by extracting or deleting only those elements of an existing list (array) that satisfy certain conditions, or by performing replacements or conversions, use list comprehensions.

The following is explained here, along with sample code.

  • Basic form of list comprehension notation
  • Apply the process to all elements of the list
  • Extract and delete elements from the list that meet the criteria
  • Replace or convert elements that satisfy the conditions of the list

See the following article for specific examples of lists of strings.

It is also possible to randomly extract elements that do not meet the criteria.

Note that lists can store different types of data and are strictly different from arrays. If you want to handle arrays in processes that require memory size and memory addresses or numerical processing of large data, use array (standard library) or NumPy.

The following list is an example

l = list(range(-5, 6))
print(l)
# [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]

Basic form of list comprehension notation

When generating a new list from a list, list comprehensions are simpler to write than for loops.

[expression for any variable name in iterable object if conditional expression]

An expression is applied to an element that satisfies the conditional expression of an iterable object (such as a list or tuple) and becomes an element of a new list. The “if conditional expression” can be omitted, in which case the expression is applied to all elements.

See the following article for more details, including nested list comprehension notation.

Apply the process to all elements of the list

For example, if you want to apply some processing to all the elements of a list, describe the desired processing in the list comprehension expression above.

l_square = [i**2 for i in l]
print(l_square)
# [25, 16, 9, 4, 1, 0, 1, 4, 9, 16, 25]

l_str = [str(i) for i in l]
print(l_str)
# ['-5', '-4', '-3', '-2', '-1', '0', '1', '2', '3', '4', '5']

This can be used to convert between lists of numbers and lists of strings.

Extract and delete elements from the list that meet the criteria

If the element is only to be selected by a conditional expression, it is not processed by an expression, so it takes the following form

[variable name for variable name in original list if conditional expression]

A new list is generated from which only elements that satisfy the condition (elements for which the conditional expression is true) are extracted.

l_even = [i for i in l if i % 2 == 0]
print(l_even)
# [-4, -2, 0, 2, 4]

l_minus = [i for i in l if i < 0]
print(l_minus)
# [-5, -4, -3, -2, -1]

If “if conditional expression” is set to “if not conditional expression,” it becomes a negation, and only elements that do not satisfy the condition (elements for which the conditional expression is false) can be selected and extracted. In other words, a new list is generated from which elements that satisfy the condition are removed.

l_odd = [i for i in l if not i % 2 == 0]
print(l_odd)
# [-5, -3, -1, 1, 3, 5]

l_plus = [i for i in l if not i < 0]
print(l_plus)
# [0, 1, 2, 3, 4, 5]

Of course, the same result is obtained by specifying the equivalent conditional expression without using not.

l_odd = [i for i in l if i % 2 != 0]
print(l_odd)
# [-5, -3, -1, 1, 3, 5]

l_plus = [i for i in l if i >= 0]
print(l_plus)
# [0, 1, 2, 3, 4, 5]

The conditional expression part can be multiple conditions. Negative nots can also be used.

l_minus_or_even = [i for i in l if (i < 0) or (i % 2 == 0)]
print(l_minus_or_even)
# [-5, -4, -3, -2, -1, 0, 2, 4]

l_minus_and_odd = [i for i in l if (i < 0) and not (i % 2 == 0)]
print(l_minus_and_odd)
# [-5, -3, -1]

Replace or convert elements that satisfy the conditions of the list

In the example of element extraction above, elements that did not satisfy the conditions were removed.

If you want to perform substitutions, conversions, etc. only on elements that satisfy the conditions, apply the ternary operator to the expression part of the list comprehension notation.

In Python, the ternary operator can be written as follows

True Value if Conditional Expression else False Value
a = 80
x = 100 if a > 50 else 0
print(x)
# 100

b = 30
y = 100 if b > 50 else 0
print(y)
# 0

It is a bit complicated, but the combination of list comprehension notation and ternary operators is as follows.

[True Value if Conditional Expression else False Value for any variable name in original list]

The part enclosed in parentheses is the ternary operator (parentheses are not necessary in the actual code).

[(True Value if Conditional Expression else False Value) for any variable name in original list]

If any variable name is written as is for true or false values, the value of the original element is used as is. If an expression is written, the processing of that expression is applied.

l_replace = [100 if i > 0 else i for i in l]
print(l_replace)
# [-5, -4, -3, -2, -1, 0, 100, 100, 100, 100, 100]

l_replace2 = [100 if i > 0 else 0 for i in l]
print(l_replace2)
# [0, 0, 0, 0, 0, 0, 100, 100, 100, 100, 100]

l_convert = [i * 10 if i % 2 == 0 else i for i in l]
print(l_convert)
# [-5, -40, -3, -20, -1, 0, 1, 20, 3, 40, 5]

l_convert2 = [i * 10 if i % 2 == 0 else i / 10 for i in l]
print(l_convert2)
# [-0.5, -40, -0.3, -20, -0.1, 0, 0.1, 20, 0.3, 40, 0.5]
Copied title and URL