Python

Python Multiply List by Scalar

In Python, the most elementary data building is the sequence. Each sequence element allotted a number – its index or placement. The starting point of the index is ‘0’, the second point is ‘1’, and so forth. Python offers six in-built types of sequences, but the most important or commonly used are lists, which we would discuss in this guide. Python list is the most useful data type. It can be written within a square bracket, and a comma separates every item in the list.

The most versatile and crucial thing about a list is that you can add any type of value to a list. It is not compulsory to add the same type of values to it. You can add, remove, multiply any item in the list. But today, this guide is all about how to multiply scalar with a python list. Come let’s check how the list function works in the python language. Here we will elaborate the list function by using the Spyder compiler in Windows 10.

Example 1

Our first example is to multiply scalar with a python list. A scalar is a form that uses a single value. In python, the most frequently used scalar types are float, int, complex, bool, and so forth. Now let’s check how the program works.

To launch Spyder IDE, type ‘Spyder’ in your Windows PC search bar and then click open. Create a new file from the File menu or simply use a keyboard shortcut ‘Ctrl+Shift+N.’ After creating a new file, write a python code to elaborate list multiplication by a scalar.

We will be deliberating the simplest and convenient way to multiply a list by a scalar in Python language. First, we create a list and add values to it. Our next step multiplies every item in the list by 3. Then we define a print function that prints the resultant values.

Check out the attached code:

b_list = [2, 3, 4]
multiplied_list = [value * 3 for value in b_list]
print(multiplied_list)

After you successfully write the python code, now it’s time to save your code file with the ‘.py’ extension as below. The file name may be different in your illustration.

Now run the file or simply use the “F9” shortcut key to check the output of a python list multiplication by a scalar. The output can be seen in the appended screenshot.

Example 2

In our second example, we use the traversal method to find the product of the values in the list. Now let’s check how the program works. Let’s move to the Spyder compiler in Windows 10 and select a new empty file or use the same file, “ScalarMultilication1.py”. We used the same code file, “ScalarMultilication1.py” and made changes to it.

In this program, we first define a list using the traversal method and then initialize the value by 1. This value traverses until the list ends and multiplies with every number present in the list. The value is saved in the ‘mul_result’, and at the end, it gives you your desired output. Here we use two lists, so the value traverses from one list and stores its results, then moves to another list and stores its result. At last, we use the print function, which prints the output on the console screen. Check out the attached code:

Def multiplyList(List1) :
    Mul_result = 1
    For y in List1:
        Mul_result = mul_result * y
    Result mul_result

Mullist1= [3, 2, 1]
Mullist2 = [6, 2, 1]
Print(multiplyList(mullist1)
Print(multiplyList(mullist2)

Again, save the “ScalarMultilication1.py” file for further execution. Then again, build and run the code or simply use the F9 key to check the output of a python multiplication. After compiling the above program, you will get the desired output.

Conclusion

In this tutorial, we discussed the versatility of the python list function and its implementation using the Spyder compiler. You can use any scalar types (int, float, Boolean, etc.) and any method other than traversal with the list multiplication function in python language. The first illustration was all about using a single list; however, we have used two lists in our second illustration. It is suggested to implement both illustrations on your system using a compilation tool and then make little changes to get a better understanding of python list multiplication by using scalars.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content