Perhaps you wish to pass a list through args, but this shouldn't be done so.
Why? Because length of the command line passed to the kernel is limited. Newest Linux distros accept pretty long strings, but still. When you have a lot of stuff that can contain special characters and so on, it sometimes may go terribly wrong. Especially if you will give usage of the receiver script to other people, who can forget to bypass shell and send the data directly to your script. And in this case whole mess can rise.
So, I advise you to use pipes. I.e. in receiver read from sys.stdin, and in sender use Popen().communicate().
Something like this:
# Receiver:
from cPickle import loads
import sys
data = sys.stdin.read() # This will return after '^D' is received.
data = loads(data)
print "I received:"
for x in data:
print x
print "\t", data[x]
# Sender:
from cPickle import dumps
from subprocess import Popen, PIPE
data = {"list1": [1, 2, 3, 4],
"list2": [3, 4, 5, 6]}
cmd = ["python", "receiver.py"]
p = Popen(cmd, stdin=PIPE, stdout=PIPE, stderr=PIPE)
result, error = p.communicate(dumps(data))
if error:
raise Exception(error)
print "Receiver sent:"
print result
Now, if other people need not to use Python to send data to your script, then do not use pickling but json as suggested in another answer.
Or you can even use literal_eval():
# In receiver:
from ast import literal_eval
import sys
data = sys.stdin.read()
data = literal_eval(data)
#...
# And in sender:
# ...
result, error = p.communicate(repr(data))
#...
literal_eval() is a secure version of eval() that will evaluate only built-in data types, not expressions and other objects.
If you still insist on using args, then you should use something like:
list1 = [1, 2, 3, 4]
def convert (l):
return ",".join((str(x) for x in l))
def get (s):
return s.split(",")
But then you have to make sure that your separator isn't appearing anywhere else in the string or make an escaping mechanism. Also if you would not be sending only alpha-numeric characters you would have to encode the stuff using URL-safe base64 to avoid possible clashes with OS and/or shell.