2

I have a list of elements. Each element is a list. Eg

a=[[1,2,3,[1,2,3,4]], [4,5,6,[2,5]], [7,8,9,[3,3,4]]...]]

How do I go about getting say the second element from each element in the outer list i.e. [2,5,...]?

a[:,2] would be nice (what other possible interpretation could it have) but it would seem that the only way for me to do this is with a for loop of some description. If it is the latter it would seem to me that I would be better rewriting my code with a named array for each element. I would then ask - when would a list actually be useful?

I could equally ask the same question if I used rounded brackets and replaced the term list with tuple which then leads me to ask why bother with the difference? what does a tuple give me that I cant achieve with a list?

user3799584
  • 917
  • 1
  • 9
  • 18

2 Answers2

1

If you want to avoid loops you can use itemgetter and map to extract the list:

import operator

a = [[1,2,3,[1,2,3,4]], [4,5,6,[2,5]], [7,8,9,[3,3,4]]]
print map(operator.itemgetter(1), a) # [2, 5, 8]

Depending what data your list holds you might be better off creating a class and using objects instead. Using lists gives you an ability to use list operations and allows sublists to be used as sequences and iterables which might be beneficial depending on what you want to do with the data. Same kind of functionality can be achieved with custom class and magic methods but it would require more work.

Using tuples would allow you to use them in sets and dict keys because they are immutable. There are other questions with detailed answers regarding the differences.

Community
  • 1
  • 1
niemmi
  • 17,113
  • 7
  • 35
  • 42
  • The data is basically as indicated - each element of the outer list is a list of numbers, and a variable length array of numbers. Each element represents various quantities of a particular solution. After finding all the solutions I want to select those that satisfy particular properties. A class is ok but then I have a list/array of objects and I would still have the same question – user3799584 Apr 14 '16 at 14:24
  • I was thinking about converting the inner lists to objects, If they all have the same length and number/list in each index has a specific meaning the code might be more readable if objects are used instead. You could still use `map` or list comprehension to produce a list from the elements, no matter if they are objects or lists. – niemmi Apr 14 '16 at 14:33
0

Have your tried this?

>>> a=[[1, 2, 3, [1, 2, 3, 4]], [4, 5, 6, [2, 5]], [7, 8, 9, [3, 3, 4]]]
>>> [ i[1] for i in a]
[2, 5, 8]
Robert Jacobs
  • 3,266
  • 1
  • 20
  • 30
  • Otherwise known as a for loop! I can see no reason not to allow a[:,1] as a valid construct. It is certainly a lot more readable than having a bunch of for loops throughout your code. or "list comprehension" if you prefer. – user3799584 Apr 14 '16 at 22:24
  • Complain to the language designers or write your own language. – Robert Jacobs Apr 15 '16 at 13:05
  • As my question asked - could I do it without a for loop. Since the answer is clearly no it is then better to rewrite the code and separate all variables with their own name instead of a single list. – user3799584 Apr 17 '16 at 19:30