user2277435
user2277435

Reputation: 245

Can't unpack list structure for nested list comprehension

Having issues creating a simple nested list comprehension. Each level of unpacking doesn't properly remove the list from list structure. Detail below.

file_list = ['genes1.csv', 'genes2.csv']

set_list = [('genes2.csv', (['A2LD1', 'A1BG', 'A2ML1', 'A1CF', 'A2M', 'A2BP1'], [-0.95, 1.226, 3.473, 4.958, 6.645, 11.953])), ('genes1.csv', (['A2LD1', 'A1BG', 'A2ML1', 'A1CF', 'A2M', 'A2BP1'], [-0.529, 1.444, 3.133, 4.303, 6.387, 11.117]))]

set_list.sort(key = lambda (x,y): file_list.index(x))

Let's look at the sorted set_list:

print '\nset_list sorted:', set_list

set_list sorted: [('genes1.csv', (['A2LD1', 'A1BG', 'A2ML1', 'A1CF', 'A2M', 'A2BP1'], [-0.529, 1.444, 3.133, 4.303, 6.387, 11.117])), ('genes2.csv', (['A2LD1', 'A1BG', 'A2ML1', 'A1CF', 'A2M', 'A2BP1'], [-0.95, 1.226, 3.473, 4.958, 6.645, 11.953]))]

Now we're going to pull values only for sample 1, in this case, values from genes1.csv:

sample_1_values = []
for i, (j, k) in set_list[:1]:
    for v in k:
        sample_1_values.append(v)

print '\nsample 1 values:', sample_1_values

And the output:

sample 1 values: [-0.529, 1.444, 3.133, 4.303, 6.387, 11.117]

Now the aim is to create sample_1_values as a nested list comprehension:

sample_1_values = [[v for v in k] for i, (j, k) in set_list[:1]]

print '\nsample_1_values from list comprehension:', sample_1_values

And the output:

[[-0.529, 1.444, 3.133, 4.303, 6.387, 11.117]]

Let's break down the nesting structure, first:

s_list_comp_1 = [k for i, (j, k) in set_list[:1]]

print '\ninner list comprehension:', s_list_comp_1
print type(s_list_comp_1)

Output:

inner list comprehension: [[-0.529, 1.444, 3.133, 4.303, 6.387, 11.117]]
<type 'list'>

No unpacking has occurred. Now for the outer nesting:

s_list_comp_2 = [v for v in s_list_comp_1]

print '\nouter list comprehension', s_list_comp_2
print type(s_list_comp_2)

Output:

[[-0.529, 1.444, 3.133, 4.303, 6.387, 11.117]]
<type 'list'>

The same issue, the list hasn't been unpacked this time. With a for statement and append, we see it does properly:

s_list_comp_2 = []
for v in s_list_comp_1:
    for l in v:
        s_list_comp_2.append(l)

print '\ncorrect unpacking:', s_list_comp_2

Output:

correct unpacking: [-0.529, 1.444, 3.133, 4.303, 6.387, 11.117]

The correct unpacking takes another level of hierarchy which I can't replicate using a nested list comprehension. The entire expression creating sample_1_values must be a nested list comprehension to insert in later without having this placeholder. Suggestions?

Upvotes: 0

Views: 375

Answers (1)

BrenBarn
BrenBarn

Reputation: 251363

If I understand you right, you want this:

[v for i, (j, k) in set_list[:1] for v in k]

In your "inner list comprehension" example, no unpacking occurs because the list comprehension simply yields the k value from each item in set_list, and that item is a list.

In your original example:

[[v for v in k] for i, (j, k) in set_list[:1]]

The items are unpacked by v for v in k, but you repack them by making a new list out of them (with a nested list comprehension).

What you want is not a nested list comprehension but simply a single list comprehension with multiple for clauses.

Upvotes: 2

Related Questions