How can I pass a list as a command-line argument with argparse?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When a Python script needs multiple values from the command line, the cleanest solution is usually to let argparse collect them as a list. The right pattern depends on how the user should type the input: as repeated positional values, repeated options, or one comma-separated string.
Use nargs For Space-Separated Values
The most common approach is to accept several values separated by spaces. argparse does this with nargs.
If the values are positional arguments:
Run it like this:
The parsed result is a real Python list:
nargs="+" means one or more values. If you want zero or more, use nargs="*".
Accept Lists On Optional Flags
The same idea works for optional arguments:
Example:
This produces:
This style is easy to document and easy for users to type because the shell already understands space-separated tokens.
Use action="append" When The Flag Can Repeat
Sometimes you want users to repeat the same option several times. In that case, append is often clearer than forcing everything into one occurrence.
Example:
Result:
This is a good fit when each value is conceptually its own flag occurrence.
Parse A Comma-Separated List Deliberately
If the calling convention requires one argument containing commas, parse that explicitly instead of using type=list, which does not do what most people expect.
Example:
Result:
This pattern is helpful when values come from shell variables or configuration wrappers that already produce a single string.
Choose The User Experience First
The best answer is not only about what argparse can parse. It is about what your users can type reliably.
Use space-separated values with nargs when:
- users type values manually
- values are simple tokens
- normal shell splitting is desirable
Use repeated flags with append when:
- each value is conceptually a separate option
- you want the command to stay readable
- values may appear conditionally
Use a comma-separated parser when:
- the caller already provides one string
- you are matching an existing CLI contract
- you want to keep the whole list inside one quoted argument
Common Pitfalls
- Using
type=listand expectingargparseto split the string automatically. - Forgetting to quote comma-separated input when the shell would otherwise split or expand it.
- Using
nargs="*"when at least one value should be required. - Mixing repeated flags and
nargswithout thinking through the resulting nested structure. - Assuming
appendandnargsproduce the same shape of parsed data.
Summary
- Use
nargsfor space-separated lists on positional or optional arguments. - Use
action="append"when the same option can appear multiple times. - For comma-separated input, parse the string yourself with a small helper function.
- Pick the pattern that matches how users will actually type the command.

