8

Could someone please explain the concept of having a comma delimited chain (probably not the correct terminology, but I didn't want to confuse it with list) of variable names on the left side of the assignment operator?

What I'm referring to would be something of the following nature

reader = csv.reader(open('some_file', 'rb'))
for row in reader:
    k, v = row
    myDictionary[k] = v

I know that example might lead to the question of the format of 'some_file', so here is another example I've come across

username, password = sys.argv[1:]

I understand that argv comes from the command line, and 1: refers to all arguments after the python script name, but how do username and password get the correct items? In other words, what delimits the arguments passed into this program, is it just the space between the arguments?

In reference to the first example, how do k and v get their values from row, assuming row is a two column line. Do those use the comma as the delimiter?

Any explanation and/or links to Python code or documentation would be great.

1
  • 2
    Relevant Python docs here Commented Apr 26, 2016 at 14:39

4 Answers 4

9
k, v = row

and

username, password = sys.argv[1:]

are examples of sequence unpacking. Sequence unpacking requires the number of variables on the left hand side of the assignment operator to have the same number of elements as the sequence on the right. If they do the first element of the sequence is assigned to the first variable, the second to the second and so on. If they are not equal it will throw a value error.

str, unicode, list, tuple, bytearray, buffer, xrange are all valid sequences and can be used on the right hand side of the operator.


All that is left to understand is whether 'row' and sys.argv[1:] are valid sequences with 2 elements.

csv.reader() returns each row of the csv as a list of strings. So it is a valid sequence. If the csv has 2 columns the list will have 2 elements. By default csv.reader uses a comma as the delimeter to split on. You can specify a different delimiter if required:

csv.reader(csv_file, delimiter='|')

For every invocation of Python, sys.argv is automatically a list of strings representing the arguments (as separated by spaces) on the command-line. So again a valid sequence. argv[0] is always the script name. So if the user ran the program with 2 arguments, a username followed by a password sys.argv[1:] would be a list of 2 elements as required.

Sign up to request clarification or add additional context in comments.

Comments

2

This works for the same reason this works:

a, b = [1, 2]

Which assigns a to 1 and b to 2.

This applies to you CSV reader as your file must have two columns and the first variable assigns to that line's first column and the second variable to the second column. The reader returns a list like above so that why it works :)

Comments

2

Basicallycsv reader return every row as a list

Now assume a list is a=[1,2,3] if you want to unpack the list by assigning that should be

a1,a2,a3 = a

and the unpacking value will assign like this to the variable

a1=1 a2=2 a3=3

the number left side assignment variable should be the equal of length of list.

if the number of variable and list length is unequal than you will get an error

ValueError: too many values to unpack

And list will unpack the value as it's index and assign it left to right variable.

Now let's come to the point. in csv reader the row is list type and the length of this list is the number of csv file column. So when you will unpack it you have to keep in mind to give the number of assign variable.

And another sys.argv[] also return a list.

And the lastly csv file delimiter is comma(,) and command parameter parser delimiter is space(one/more) but after parsing they both return a list. So we have to consider about the list when we try to unpack it ass multiple assign variable.

Comments

-1

On the left side there is a TUPLE of variables and on the right side there is a LIST of values. Each variable gets the appropriate value. So this method has nothing to do with strings and with any value separator. The keyword is LIST(s).

As for the csv file, you can get the info from pydoc csv at the definition of reader():

reader(...)
    csv_reader = reader(iterable [, dialect='excel']
                            [optional keyword args])
        for row in csv_reader:
            process(row)

    The "iterable" argument can be any object that returns a line
    of input for each iteration, such as a file object or a list.  The
    optional "dialect" parameter is discussed below.  The function
    also accepts optional keyword arguments which override settings
    provided by the dialect

.

4 Comments

This is not a list, but a tuple definition that isn't surrounded by parenthesis. See the link from heavyd above.
While you are right, I do not think that curran is at the position where he/she can/must know the (slight) difference between lists and tuples.
More specifically row is a list that if it has 2 values, has those two values copied to the two values of a tuple, in this case those are the variables k and v.
Thank you Gombai for the answer and thank you Phillip for following up. I'm tending to lean towards Phillip's point of view on this topic. The reason I didn't use list in my question, and specifically pointed out why I didn't, is because the variables aren't in a list. What I was really hoping to find was what Phillip said, that this is a tuple definition not surrounded by parenthesis.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.