
Hi! Let’s learn how to use python argparse with a comma separated list of values. We will be using Python 3.8.10. Let’s go! ⚡⚡✨✨
If you haven’t already done so, please visit our previous tutorial on python argparse HERE. It should help you understand how argparse works before jumping into this awesome tutorial.
Ok! In this example, we will use argparse to accept and calculate the sum of a comma separated list of prices. Let’s write our code:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--prices",type=str,required=True)
d = vars(parser.parse_args())
if "prices" in d.keys():
d["prices"] = [float(s.strip()) for s in d["prices"].split(",")]
sum = 0
for p in d["prices"]:
sum += p
print(f"The total is: {sum:.2f}")
Let’s explain what is happening here:
- We import the argparse library first, as usual.
- Method argparse.ArgumentParser() creates a new ArgumentParser object. The new object is called parser.
- add_argument() defines how a single command line argument should be parsed. The name of the argument is –prices, the type is str and the required flag is True. Note that the name of the argument must include 2 hyphens and not one.
- parse_args() converts argument strings to objects. In this case, we nest this call in the method vars. Recall that the method vars returns the __dict__ attribute of the supplied object. In this case it returns the __dict__ attribute of the parser object parser as d. Object d will be a dictionary object that contains a key value pair with the key value prices (our argument) and the comma separated values supplied for argument prices.
- We check to see if indeed our dictionary object d contains the key value prices. If so, we split up the values of the comma separated list into separate numbers using the comma as a delimiter and strip extra whitespace from each value. After this step is complete, we will have a clean dictionary that contains each value of the comma separated list as a separate value of type float.
- We declare a new variable sum which will contain the sum of the comma separated values.
- We calculate the sum of all the values in our dictionary.
- Print the value to the screen formatted to two decimal places.
Because we used argparse, we must type the correct command at the command line in order for our script to do its job. See the below screenshot as a guide:

Let’s explain what is happening here:
- Ideally, you’d first open a new cmd window on Windows 10 or a new Terminal on Linux/Mac and type something very similar to the above command.
- The python executable name is usually python but this may differ depending on how you configured your system and python interpreter.
- The python script name is the name of the script file. On my system, the filename is PYTHON_ARGPARSE_COMMA.py. Ensure that your current directory contains this file and that you spell it correctly.
- The argument name as defined in our code is prices, with 2 hyphens. This is what we must type at the command line. If you misspell this or leave this out, you will get an error.
- The value of the parameter is a comma separated list of values with no spaces. This is the actual value of the parameter. If you omit this, you will get an error. If you do not use a comma to separate the values, you will also get an error. You may use as many or as few comma separated values as you wish, but it must have at least one value.
- Make sure that you put one space between each part of the above command when you practice this on your own machine.
If all goes well, when you execute this script you will get something similar to the following output (different totals depending on your list of comma separated values):
#output
#The total is: 111.71
Awesome! So we were able to use python argparse to create a comma separated list of values. You can find the full argparse documentation HERE.
Thanks for reading and good luck!

Are you worried about your child’s online safety or your employees’ productivity? Do you wonder what they’re accessing on their devices? SentryPC is here to address these concerns. This all-in-one, cloud-based software provides robust activity monitoring, content filtering, and time management, making it ideal for both parental control and employee monitoring. Embrace peace of mind and enhanced efficiency with SentryPC, the proactive solution to your digital monitoring needs. CLICK HERE to get started.