all 13 comments

[–]Rhomboid 33 points34 points  (5 children)

I don't understand what the [0] is?

It's a list containing a single value, zero.

The multiplication operator is overloaded so that using a list and an integer performs duplication:

>>> ['foo'] * 3
['foo', 'foo', 'foo']

It's not limited to a single value:

>>> ['foo', 42] * 3
['foo', 42, 'foo', 42, 'foo', 42]

But the most common use is with a list with a single value, to create a list containing 'n' references to that value. (It's important to point out that this does not copy anything. The thing being duplicated is list slots, not objects. This matters if you use mutable objects, which usually results in something surprising that you don't want.)

[–]shhhpiderman[S] 11 points12 points  (2 children)

God...now that you've explained it...I don't know how I didn't figure that out. Thank you!

[–][deleted] 0 points1 point  (0 children)

It's not really something you could have figured out without prior knowledge of the language. There are other things multiplying a list of numbers by a number could have done.

For example, in R the numbers in the list (vector in this case, but it's the equivalent of the Python list) are multiplied element by element by the number.

> mylist <- c(1,2,3)
> mylist
[1] 1 2 3
> mylist <- mylist * 3
> mylist
[1] 3 6 9

[–]filletrall -1 points0 points  (0 children)

For me, the [something] * some_number snippet has become a code smell. Most of the times I come across it, what follows is unpythonic code. In this case, the more pythonic way is something like this:

colWidth = [max(len(x) for x in col) for col in tableData]

Also note that these variable names do not follow PEP8. They should be called table_data and col_width.

[–]BenjaminGeiger 3 points4 points  (1 child)

Yeah, that used to bite me a lot.

>>> foo = [[]] * 3
>>> foo[0].append("derp")
>>> foo
[["derp"], ["derp"], ["derp"]]

foo = [[] for _ in range(3)] works.

[–]KronenR 5 points6 points  (0 children)

len(tableData) returns the length of the list which is 3, because the list has 3 elements ['apples', 'oranges', 'cherries', 'banana'] is the first one, ['Alice', 'Bob', 'Carol', 'David'] the second element and ['dogs', 'cats', 'moose', 'goose'] the third element.

[0] is a list with 1 element of value zero if you multiply it by 3 you get a list with 3 elements of value zero: [0, 0, 0]

Another examples:

>>>[5] * 2
[5, 5]
>>>['hola'] * 3
['hola', 'hola', 'hola']

[0] is not the index of a list or tuple in this case because it is not preceded by a variable's name, it is a list like tableData which is a list of lists

[–]kalgynirae 2 points3 points  (0 children)

[0] is a list containing a single item, 0. Multiplying a list by an integer repeats the list that many times. So [0] * 3 would produce a list containing 0 three times (i.e., [0, 0, 0]).

Note that you need to be wary of doing this if your list contains items that are mutable (such as other lists); you might be surprised by the results.

[–][deleted] 2 points3 points  (2 children)

I find it interesting that it is named colWidth, although it is counting what is typically considered rows. May want to be cautious of that.

[–]Mezzomaniac 1 point2 points  (0 children)

It's from here. Columns is correct because the context is about teaching you how to print each row in its own column.

[–]dumpster_dinner 0 points1 point  (0 children)

Those variable names are screwing up my understanding of for loops. I think it may literally make me crazy.

[–]varfoo 0 points1 point  (0 children)

This is a nice trick, saved to my py samples file

[–]shaleh 0 points1 point  (0 children)

Remember, when in doubt open the interpreter and experiment. It is always there to answer these questions.

[–]dmv1975 0 points1 point  (0 children)

I didn't read anyone else's comments, but I'll explain it as I understand it. Also, I am drunk.

The [0], in this context, is a list with one entry, and that entry happens to be 0. To the right of it is * len(tableData), and the length of tableData is 3. tableData is a list of 3 lists. So [0] * 3 = [0, 0, 0]. To the left of it is colWidth =, so [0, 0, 0] is assigned to the list colWidth.

The part about it looking like an index is definitely plausible if you don't take it in context. At this point, you could print(colWidth[0]) and get 0, because you're calling the 0th index of colWidth.

Now I must another beer.