Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access tuple elements in a nested list [duplicate]

I have a list with nested lists, which contain tuples. The list looks like this:

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

I would like to be able access the "None" elements of the tuple, per index value. I would like to create a new list with the same index values that would look like:

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

I don't really care what type the "None" is in. I just want them as a separate list.

I've tried list comprehensions, but I only can retrieve the tuples themselves, not the elements inside.

Any help would be appreciated.

like image 832
user1882766 Avatar asked Feb 13 '13 22:02

user1882766


People also ask

How do you access elements in nested tuples?

The nested tuple with the elements (100, 200, 300) can be retrieved by using tuple name with the index value i.e. tup[index] and each element of the nested tuple can be accessed by using tup[index-1][index-2].

How do you find duplicate tuples in a list Python?

Initial approach that can be applied is that we can iterate on each tuple and check it's count in list using count() , if greater than one, we can add to list. To remove multiple additions, we can convert the result to set using set() .

How do you traverse a nested tuple in Python?

Easiest way is to employ two nested for loops. Outer loop fetches each tuple and inner loop traverses each item from the tuple. Inner print() function end=' ' to print all items in a tuple in one line. Another print() introduces new line after each tuple.


1 Answers

Simplest for just a single list containing tuples would be:

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

Or if it's 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 these examples below are using nested lists. It's a list of lists that contain tuples. I figured that's what you were looking for since you were showing two variations of the lists.

Use a nested comprehension list:

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 variations:

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's 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 nested list comprehension

like image 89
Roy Nieterau Avatar answered Nov 14 '22 21:11

Roy Nieterau