That’s not what that expression does. lista[inicio:fim]
creates a sublist started (including) in inicio
and terminated (exclusively) in fim
:
>>> [0,1,2,3,4,5][1:4]
[1, 2, 3]
>>> [0,1,2][0:3]
[0, 1, 2]
Note that in this second example the list was the same (because you started at its beginning and went all the way. Similarly, in your code the returned sublist is being equal to the original matrix, and by accessing your second element you are actually taking the second line:
>>> matriz = [[1,5],
... [7,4],
... [8,3]]
>>> matriz[0:3]
[[1, 5], [7, 4], [8, 3]]
>>> matriz[0:3][1]
[7, 4]
If you want the second element of each matrix row, you can use an understanding of lists:
>>> [x[1] for x in matriz]
[5, 4, 3]
Then just add a loop to print them one by one:
>>> for v in [x[1] for x in matriz]:
... print v
...
5
4
3
(although in this case it is silly to do this, just iterate on the original list and print v[1]
...)
By the way, if instead of a list you have an object, you can use other complex objects (such as tuples) as keys. Then it would make sense something like matriz[0, 1]
, but it would be accessing a specific property of the object and not two distinct values of a nested list:
>>> x = []
>>> x[1, 2] = 10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: list indices must be integers, not tuple
>>> x = {}
>>> x[1, 2] = 10
>>> x
{(1, 2): 10}
But if what you have are lists of lists, in fact it takes two uses of the brackets to access an internal element, you cannot (unfortunately...) use tuples as indexes.
Note: if it is sorely needed, you could put the print
within the very understanding of lists, avoiding a loop. In Python 3 at least, I don’t know if you can do this in Python 2:
>>> [print(x[1]) for x in matriz]
5
4
3
[None, None, None]
(this last line is the list created, where each element is the return of the print
- None
; normally it will be ignored)
Just be careful that this creates a new list, the same size as the original (1st level), which increases memory consumption. This technique would only be recommended if you were in a context where only one expression fits (within a lambda
, for example) and - concisely - you wanted to avoid creating a new function.
The print example is quite dirty. Why create a list with multiple None objects? While a list comprehension works well by applying another function, in the case of print it makes no sense at all, being more gambiarra and laziness to write two lines instead of one.
– ppalacios
@Pablopalaces Several languages have a "for-each" that allows you to apply a function to a collection in a single expression (e.g., Javascript -
matriz.forEach(function(e) { console.log(e[1]); })
). Python has no such built-in, So yes, it is a trick... The question that needs to be asked is whether this trick is useful/necessary or not. As you would for example if you need to include this loop inside a lambda?– mgibsonbr
Which loop do you refer to? My point is, if the goal is to simply make a print, one is traditional is more appropriate. It makes no sense to generate a list (or any object) if later it will not be used, even more of None objects.
– ppalacios
@Pablopalaces I understand, my point is that if you need to do such an operation in a context where you expect only an expression - and not a set of instructions (statements) - You can’t. The fact that there is a way to do this, even if it is done by the police, is something that increases the expressiveness of the language. But it is logical that if you are in a context where an instruction is expected, it is best to opt for clarity, as you suggest (and in that case I would not use any list understanding, only one
for
where if theprint
usesx[1]
).– mgibsonbr
I did, but as long as that context is not the issue, I wouldn’t put that kind of example where someone with little experience can just copy and paste out of context. Imagine if the OP has a giant list, at the end of that he ends up with two giant lists can get problems with that.
– ppalacios
@Pablopalaces You’re right! I edited the answer by moving this piece pro final and explaining its purpose and limitations better.
– mgibsonbr