How to access tuple elements in a nested list

I have a list with nested lists that contain tuples. The list is as follows:

428 [(' whether', None), (' mated', None), (' rooster', None), ('', None)] 429 [(' produced', None), (' without', None), (' rooster', None), (' infertile', None), ('', None)] 

I would like to have access to the "No" elements of the tuple, for each index value. I would like to create a new list with the same index values ​​that would look like this:

 428 [(None, None, None, None)] 429 [(None, None, None, None, None)] 

I don’t care what type of "No". I just want them to be a separate list.

I tried checking the lists, but I can only get those tuples, not the elements inside.

Any help would be appreciated.

+4
source share
6 answers

The easiest way for a single list containing tuples would be:

 [x[1] for x in myList] # [None, None, None, None] 

Or, if it is always the last value in the tuple (if it contains more than two values):

 [x[-1] for x in myList] # [None, None, None, None] 

Note that the examples below use nested lists. This is a list of lists containing tuples. I realized what you were looking for since you were showing two versions of the lists.

Use the list of nested concepts:

 myList =[ [(' whether', None), (' mated', None), (' rooster', None), ('', None)] , [(' produced', None), (' without', None), (' rooster', None), (' infertile', None), ('', None)] ] print [[x[1] for x in el] for el in myList] # [[None, None, None, None], [None, None, None, None, None]] 

Or some other options:

 myList =[ [(None, None), (' mated', None), (' rooster', None), ('', None)] , [(' produced', None), (' without', None), (' rooster', None), (' infertile', None), ('', None)] ] # If there are multiple none values (if the tuple isn't always just two values) print [ [ [ x for x in z if x == None] for z in el ] for el in myList ] # [[[None, None], [None], [None], [None]], [[None], [None], [None], [None], [None]]] # If it always the last value in the tuple print [[x[-1] for x in el] for el in myList] # [[None, None, None, None], [None, None, None, None, None]] 

Also see: SO: Understanding Understanding Nested List

+8
source

You can access elements within a tuple in the same way as with list elements: using indexes. For instance:

 lst = [1, (2, 3)] lst[1][1] # first index accesses tuple, second index element inside tuple => 3 
+5
source

If you just want to get None if it exists in the tuple:

tuple([None for t in list if None in t])

This recreates the tuple containing None for each tuple in which it is located. Please note that this would not be a good solution if you would like to get None.

0
source

What you showed is not really a list, and it's hard to guess what might be on the list itself. But I'm going to assume this is something like this:

 list_o_lists = [428, [(' whether', None), (' mated', None), (' rooster', None), ('', None)], 429, [(' produced', None), (' without', None), (' rooster', None), (' infertile', None), ('', None)]] 

Any understanding of the list for accessing tuples inside this would already be pretty terrible:

 [[tup for tup in lst] if is_sequence(lst) else lst for lst in list_o_lists] 

But changing it to access the second element of each tuple is trivial:

 [[tup[1] for tup in lst] if is_sequence(lst) else lst for lst in list_o_lists] 

In fact, no matter what your list understands, no matter how horrible it is, based on your question, somewhere you have each tuple as an expression, which means that all you have to do is put a [1] on this expression.


From your comment:

Sorry, numbers are index values.

I think you actually have something simpler:

 list_o_lists = [ [(' whether', None), (' mated', None), (' rooster', None), ('', None)], [(' produced', None), (' without', None), (' rooster', None), (' infertile', None), ('', None)]] 

And then your understanding of the list of attempts was probably something like this:

 [[tup for tup in lst] for lst in list_o_lists] 

Of course, this is just an assumption because you still have not shown us your actual list or understanding of the list that you tried. But, as I said above: "... no matter what your list understands ... somewhere you have every tuple as an expression, which means that all you have to do is put [1] into that expression. "

So this one is just as easy to change as above:

 [[tup[1] for tup in lst] for lst in list_o_lists] 

And if this is not what you really have, then what you really will also be just as easy to change. But you will have to do it yourself, because we all could not repeat our attempts to read your mind, as long as you have your actual code in front of you.

0
source

use zip with * to expand the list in args:

 x = [(' whether', None), (' mated', None), (' rooster', None), ('', None)] stuff, nones = zip(*x) # prints: stuff #(' whether', ' mated', ' rooster', '') print nones # prints: (None, None, None, None) 

zip works by taking a bunch of lists and adding the first element of each argument to the list and the second to the ect list

asterisks (*) expand the list, so you can represent zip (* x) as zip (x [0], x [1], x [2], ... x [n])

0
source

I believe that 428 and 429 are indices not in the list.

So, given the question

 li = [[(' whether', None), (' mated', None), (' rooster', None), ('', None)], [(' produced', None), (' without', None), (' rooster', None), (' infertile', None), ('', None)] ] print [ [len(subli)*(None,)] for subli in li] 

will do the job.

 [[(None, None, None, None)], [(None, None, None, None, None)]] 

Your question is strange. What will be the use of such data?

0
source

All Articles