Pass list as command line argument in Python

30 Jan, 2020
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.
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 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.pyimport sys    print("the name of the program is ", sys.argv[0])print("argument list :", sys.argv)Output:Calling a list using the command lineimport 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 formimport 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 listAlso, keep in find to change the members to the required data type if not string.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 += iprint("sum of all the list members is ", b)Output:Working with multiple list retrieved through command line.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 += iprint("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:
Working with command line: Consider the below code written in cmdlis.pyimport sys    print("the name of the program is ", sys.argv[0])print("argument list :", sys.argv)Output:
import sys    print("the name of the program is ", sys.argv[0])print("argument list :", sys.argv)
Output:
Calling a list using the command lineimport sys    print ("the name of the program is ", sys.argv[0])  a = sys.argv[1]print (a)Output:
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 formimport 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 listAlso, keep in find to change the members to the required data type if not string.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 += iprint("sum of all the list members is ", b)Output:
Program to take list as command line argument and convert it to a proper list formimport 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:
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 listAlso, keep in find to change the members to the required data type if not string.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 += iprint("sum of all the list members is ", b)Output:
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 += iprint("sum of all the list members is ", b)
Output:
Working with multiple list retrieved through command line.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 += iprint("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:
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 += iprint("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:
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.