## Problem Formulation and Solution Overview

## Method 1: Use List Comprehension

This method uses `List Comprehension`

to apply a mathematical operation to each element and return the result.

prime_nums = [2, 3, 5, 7, 11] mult_result = [x * 2 for x in prime_nums] print(mult_result)

Above declares the first (5) Prime Numbers and saves this `List`

to `prime_nums`

. Next, `List Comprehension`

loops through each element and applies the multiplication operation to each. The output saves to `mult_result`

and is output to the terminal.

`[4, 6, 10, 14, 22]` |

## Method 2: Use Pandas tolist()

This method requires an additional library to be imported, `Pandas`

, to use the `tolist()`

function.

import pandas as pd prime_nums = [2, 3, 5, 7, 11] mult_result = pd.Series(prime_nums) mult_result = (mult_result*2).tolist() print(mult_result)

Above, imports the `Pandas`

Library. Click here if this requires installation. Then, the first (5) Prime Numbers are declared and saved to `prime_nums`

.

Next, `prime_nums`

is passed as an argument to the `pd.Series()`

function and returns `mult_result`

. The output of `mult_result`

at this point is shown below.

`0 2` |

Now, we need to convert this output to a list (`tolist()`

) and apply the multiplication operation to each element. The results save to `mult_result`

and are output to the terminal.

`[4, 6, 10, 14, 22]` |

## Method 3: Use map and lambda Functions

This method wraps the `map()`

, and `lambda`

functions inside a Python `List`

and calculates the results.

prime_nums = [2, 3, 5, 7, 11] mult_result = list(map(lambda x: x*2, prime_nums)) print(mult_result)

Above declares the first (5) Prime Numbers and saves them to `prime_nums`

. The next line does the following:

- The
`map()`

function is passed the`lambda()`

function as an argument`(map(lambda x: x*2, prime_nums)`

). - The
`lambda`

performs the multiplication operation to each element of`prime_nums`

and saves it to`map()`

as an object similar to below.`<map object at 0x000001DC99CBBBB0>`

- The
`map()`

object is then converted to a`List`

. - The results save to
`mult_result`

.

Then, `mult_result`

is output to the terminal.

`[4, 6, 10, 14, 22]` |

## Method 4: Use Numpy Array()

This method requires an additional library to be imported, NumPy, to use the `np.array()`

function.

import numpy as np prime_nums = [2, 3, 5, 7, 11] the_result = list(np.array(prime_nums) * 2) print(the_result)

Above, imports the NumPy Library. Click here if this requires installation. Then the first (5) Prime Numbers are declared and saved to `prime_nums`

.

Next, `prime_nums`

is passed as an argument to `np.array()`

where the multiplication operation is applied to each element. Then, this is converted to a List, saved to `the_result`

and output to the terminal.

`[4, 6, 10, 14, 22]` |

## Method 5: Use Slicing

This method uses Python’s infamous `Slicing`

! No overhead, and a very pythonic way to resolve the issue.

prime_nums = [2, 3, 5, 7, 11] prime_nums[:] = [x * 2 for x in prime_nums] print(prime_nums)

Above declares the first (5) Prime Numbers and saves them to `prime_nums`

.

Then slicing is applied and used in conjunction with `List Comprehension`

to apply the multiplication operation to each element. The results save back to `prime_nums`

and are output to the terminal.

`[4, 6, 10, 14, 22]` |

πA Finxter Favorite!

## Summary

## Programmer Humor

π±ββοΈ **Programmer 1**: We have a problem

π§ββοΈ **Programmer 2**: Letβs use RegEx!

π±ββοΈ **Programmer 1**: Now we have two problems

… yet – you can easily reduce the two problems to zero as you polish your “RegEx Superpower in Python“. π

At university, I found my love of writing and coding. Both of which I was able to use in my career.

During the past 15 years, I have held a number of positions such as:

In-house Corporate Technical Writer for various software programs such as Navision and Microsoft CRM

Corporate Trainer (staff of 30+)

Programming Instructor

Implementation Specialist for Navision and Microsoft CRM

Senior PHP Coder