Pass list as command line argument in Python Last Updated : 30 Jan, 2020 Comments Improve Suggest changes Like Article Like Report The arguments that are given after the name of the program in the command line shell of the operating system are known as Command Line Arguments. Python provides various ways of dealing with these types of arguments. One of them is sys module. sys Module A module is a file containing Python definitions and statements. The sys module provides functions and variables used to manipulate different parts of the Python runtime environment. This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. sys.argv sys.argv is used in python to retrieve command line arguments at runtime. For a program to be able to use it, it has to be imported from the "sys" module. The arguments so obtained are in the form of an array named sys.argv. Note: sys.argv[0] gives the name of the program and the following indices work like members of an array. Approach: The name of the program is "cmdlist.py". Working with command line: Consider the below code written in cmdlis.py Python3 import sys print("the name of the program is ", sys.argv[0]) print("argument list :", sys.argv) Output: Calling a list using the command line Python3 import sys print ("the name of the program is ", sys.argv[0]) a = sys.argv[1] print (a) Output: Working with the list called by command line. sys.argv is a string array. Thus, any argument passed to it is a string so in order to properly put in use we have to convert it to an appropriate list form. Program to take list as command line argument and convert it to a proper list form Python3 import sys print("the name of the program is ", sys.argv[0]) n = len(sys.argv[1]) a = sys.argv[1][1:n-1] a = a.split(', ') for i in a: print(i) Output: Program to find the sum of all the members of the list Also, keep in find to change the members to the required data type if not string. Python3 import sys print ("the name of the program is", sys.argv[0]) n=len(sys.argv[1]) a=sys.argv[1][1:n-1] a=a.split(',') A = [int(i) for i in a] b = 0 for i in A: b += i print("sum of all the list members is ", b) Output: Working with multiple list retrieved through command line. Python3 import sys print ("the name of the program is ", sys.argv[0]) n = len(sys.argv[1]) a = sys.argv[1][1:n-1] a = a.split(', ') A = [int(i) for i in a] b = 0 for i in A: b += i print("sum of all the list members is ", b) n = len(sys.argv[2]) c = sys.argv[2][1:n-1] c = c.split(', ') d = "" for i in c: d = d+i print ("sum of all the list members is ", d) Output: Comment More infoAdvertise with us Next Article Pass list as command line argument in Python V vanshikagoyal43 Follow Improve Article Tags : Python python-utility Practice Tags : python Similar Reads Command Line Arguments in Python The arguments that are given after the name of the program in the command line shell of the operating system are known as Command Line Arguments. Python provides various ways of dealing with these types of arguments. The three most common are: Table of ContentUsing sys.argvUsing getopt moduleUsing a 5 min read How to pass a list as a command-line argument with argparse? Argparse is a Python library used to parse command-line arguments in a user-friendly manner. It makes it easy to write user-friendly command-line interfaces, and it is widely used in Python applications. In this tutorial, we will discuss how to pass a list as a command-line argument using the Argpar 5 min read Python - Passing list as an argumnet When we pass a list as an argument to a function in Python, it allows the function to access, process, and modify the elements of the list. In this article, we will see How to pass a list as an argument in Python, along with different use cases. We can directly pass a list as a function argument.Pyt 2 min read Command-Line Option and Argument Parsing using argparse in Python Command line arguments are those values that are passed during the calling of the program along with the calling statement. Usually, python uses sys.argv array to deal with such arguments but here we describe how it can be made more resourceful and user-friendly by employing argparse module. Python 7 min read Pass Arguments to the Metaclass from the Class in Python Metaclasses in Python provide a powerful way to control the creation and behavior of classes. They act as the "class of a class" and allow you to customize class creation and behavior at a higher level. One interesting aspect of metaclasses is the ability to pass arguments from a class to its metacl 3 min read Passing Dictionary as Arguments to Function - Python Passing a dictionary as an argument to a function in Python allows you to work with structured data in a more flexible and efficient manner. For example, given a dictionary d = {"name": "Alice", "age": 30}, you can pass it to a function and access its values in a structured way. Let's explore the mo 4 min read Python | How to Parse Command-Line Options In this article, we will discuss how to write a Python program to parse options supplied on the command line (found in sys.argv). Parsing command line arguments using Python argparse module The argparse module can be used to parse command-line options. This module provides a very user-friendly synta 3 min read Pass function and arguments from node.js to Python Prerequisites: How to run python scripts in node.js using the child_process module. In this article, we are going to learn how to pass functions and arguments from node.js to Python using child_process. Although Node.js is one of the most widely used web development frameworks, it lacks machine lear 4 min read Python | Set 6 (Command Line and Variable Arguments) Previous Python Articles (Set 1 | Set 2 | Set 3 | Set 4 | Set 5) This article is focused on command line arguments as well as variable arguments (args and kwargs) for the functions in python. Command Line Arguments Till now, we have taken input in python using raw_input() or input() [for integers]. 2 min read Python | Execute and parse Linux commands Prerequisite: Introduction to Linux Shell and Shell Scripting Linux is one of the most popular operating systems and is a common choice for developers. It is popular because it is open source, it's free and customizable, it is very robust and adaptable. An operating system mainly consists of two par 6 min read Like