How to access elemenst in a list of lists?

  • Thread starter Chris Roy-Smith
  • Start date
C

Chris Roy-Smith

Just learning python.
I can see that I can address an individual element of a list of lists by
doing something like:
row = list[5]
element = row[3]

But is there a way to directly address an entry in a single statement?
Thanks for any help.
Regards
Chris Roy-Smith
 
C

Chris Angelico

Just learning python.
I can see that I can address an individual element of a list of lists by
doing something like:
row = list[5]
element = row[3]

But is there a way to directly address an entry in a single statement?

Yep!

element = list[5][3]

Everything's an expression, there's nothing special about something
that's stored in a variable. You can even use square brackets after a
function call that returns a list (which is very useful at times).

Chris Angelico
 
J

James Mills

Just learning python.
I can see that I can address an individual element of a list of lists by
doing something like:
row = list[5]
element = row[3]

This is the correct approach.

Here's an interactive example (tested):

$ python
Python 2.7.1 (r271:86832, Feb 8 2011, 10:07:16)
[GCC 4.5.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
grid = [[" " for x in range(5)] for y in range(5)]
from pprint import pprint
pprint(grid)
[[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ']]
[[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' '],
[' ', ' ', 'X', ' ', ' '],
[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ']]
cheers
James
 
C

Chris Roy-Smith

Just learning python.
I can see that I can address an individual element of a list of lists by
doing something like:
row = list[5]
element = row[3]

This is the correct approach.

Here's an interactive example (tested):

$ python
Python 2.7.1 (r271:86832, Feb 8 2011, 10:07:16)
[GCC 4.5.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
grid = [[" " for x in range(5)] for y in range(5)]
from pprint import pprint
pprint(grid)
[[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ']]
grid[2][2] = "X"
pprint(grid)
[[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' '],
[' ', ' ', 'X', ' ', ' '],
[' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ']]
cheers
James
Thanks, that has my understanding problem fixed! And answers what would
probably be my next question.

Regards
Chris Roy-Smith
 
T

Terry Reedy

apple = [["a","b","c"],[1,2,3,4,5,6],["antony","max","sandra","sebastian"]]

apple[0] = ["a","b","c"]
apple[1] = [1,2,3,4,5,6]
apple[2] = ["antony","max","sandra","sebastian"]

apple[0][1] = "b"
apple[2][3] = "sebastian"

to view all videos in a loop so you can set:

for i in range(len(apple)):
print apple
for j in range (len(apple)):
print apple[j]


While this illustrate double indexing, it can be simplified to

for top in apple:
print(top)
for inner in top:
print(inner)

and give the same output. Indexing is only needed for random access and
for changing a list in place.
 
T

Terry Reedy

The method of double indexing in the manner
a[j]
for the (i, j) -th element of multi-dimensional array is well known and
widely used. But how to enable the "standard" matrix notation
a[i, j]
in Python 3.2 in the manner of numpy (and other matrix packages)? Is it
subclassing of "special methods"

__getitem__() # to get

__setitem__() # to set

Yes.

class listwrap:
def __init__(self, lis):
self._list = lis
def __getitem__(self, dex):
i,j = dex
return self._list[j]
# __setitem__: exercise for reader

l = listwrap([[1,2,3],['a','b','c']])
print(l[0,2],l[1,0])
# 3 a

IMO, Hardly worth it for two dimensions.
 
T

Terry Reedy

class listwrap:
def __init__(self, lis):
self._list = lis
def __getitem__(self, dex):
i,j = dex
return self._list[j]

# __setitem__: exercise for reader
l = listwrap([[1,2,3],['a','b','c']])
print(l[0,2],l[1,0])
# 3 a

Thank you for your response. I have to confess that I do have the cludge
of an answer to my own quesion, but it is a cludge; Your method looks
much better, though I don't think it is complete - this subclassing of
__getitem__ appears to stop simple list access, i.e. if li = [1, 2 ,3],
it seems to me that print(li[2]) would raise an exception, no?

Yes, there really should be a guard condition: if isinstance(x tuple)
or try: len(x) == 2. A __getattr__ method could be added to forwar other
list methods to the wrapped list. One could try subclassing instead of
wrapping, but some special methods have a fast path access for builtins
that bypasses subclass methods. I think __getitem__ may be one such.
Wrapping is safe in that respect.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,058
Members
48,769
Latest member
Clifft

Latest Threads

Top