For a simulation scenario I'm generating 2 random lists.
For example:
my_list_1 = [17, 4, 22, 10, 18, 19, 23, 12]
my_list_2 = [82]
Just double checked the type of my_list_2 and the result is <class 'list'>
I need to create a new_list_3 with the result of 82*17, 82*4, 82*22, etc.
I've tried this way:
result_list_3 = []
for i in (my_list):
result_list_3.append(i * my_list_2)
And I'm getting the error:
TypeError: can't multiply sequence by non-int of type 'list'
Then I made a dictionary and a pd.DataFrame to try the following:
df['result_list_3'] = df['my_list_1'] * df['my_list_2']
...and I get an giant list of integers, which obviously is not the result I'm looking for.
Define my_list_1
, my_list_2
as np.array
as follows:
my_list_1 = np.array([17, 4, 22, 10, 18, 19, 23, 12])
my_list_2 = np.array([82])
Then, we can use the broadcasting
feature of numpy array like below:
my_list_3 = my_list_1 * my_list_2
You will get:
print(my_list_3)
array([1394, 328, 1804, 820, 1476, 1558, 1886, 984])