# [Baypiggies] nested list question

Jason Culverhouse jason at mischievous.org
Wed Jun 29 07:06:39 CEST 2011

```
On Jun 28, 2011, at 8:43 AM, Vikram K wrote:

> Thanks Jason. Could you (or someone else) suggest some approach for the following:
>
> >>> x
> [['19600894', '1', 'chr15_76136768', 'MISSENSE'], ['19600894', '2', 'chr15_76136768', 'MISSENSE'], ['18467762', '1', 'chr14_23354066', 'MISSENSE']]
>
>
> How do i obtain from nested list x (given above), the following nested list z:
>
> >>> z
> [[19600894','1/2','chr15_76136768', 'MISSENSE', 'homozygous'], ['18467762', '1','chr14_23354066', 'MISSENSE', 'heterozygous']]
>
> In list x, the first element is loci, second element is allele, third element is chromosome_positionofchange, fourth is type of change. Based on the value of the second and third element a new element has to be created --'homozygous' if both allele 1 and allele 2 have the change and 'heterozygous' if only one allele has the change.
>
>

Just for kicks... Is this an employment test?

Does anyone have a better way to code the inside of the for loop?
----
from operator import itemgetter
from itertools import groupby

from somewhere import unique_justseen # http://docs.python.org/library/itertools.html

key_func = itemgetter(0,2,3)

output = []
# you need to sort to make group by work properly
for k, v in groupby(sorted(x, key=key_func), key_func):
#these are sorted to unique_justseen is a good option
# as long as there are not that many allele
inner = list(unique_justseen(v))
output.append([k[0], '/'.join(i[1] for i in inner), k[1], k[2], len(inner) and 'homozygous' or 'heterozygous'])

print output

[['18467762', '1', 'chr14_23354066', 'MISSENSE', 'homozygous'], ['19600894', '1/2', 'chr15_76136768', 'MISSENSE', 'homozygous']]

Jason

>
> On Mon, Jun 27, 2011 at 6:29 PM, Jason Culverhouse <jason at mischievous.org> wrote:
> On Jun 27, 2011, at 2:06 PM, Vikram K wrote:
>
>> Suppose i have the following nested list:
>>
>> >>> x
>> [['19600894', '1', 'chr15_76136768', 'MISSENSE'], ['19600894', '2', 'chr15_76136768', 'MISSENSE'], ['18467762', '1', 'chr14_23354066', 'MISSENSE']]
>>
>>
>> How do i obtain from nested list x (given above), the following nested list z:
>>
>> >>> z
>> [['chr15_76136768', 'MISSENSE'], ['chr14_23354066', 'MISSENSE']]
>>
>
>
> 	list(unique_everseen((y[2:4] for y in x), operator.itemgetter(0)))
>
> or the whole nested list with just
>
> 	list(unique_everseen(x, operator.itemgetter(2)))
>
>  where :
>
> 	unique_everseen is from
> 	http://docs.python.org/library/itertools.html
>
> If you data is already sorted by the key then
> 	unique_justseen
>
> might be more efficient?
>
> Jason
>
>> ------
>> In other words, if the third element of an element of x is the same, then i wish to combine it into a single element.
>> _______________________________________________
>> Baypiggies mailing list
>> Baypiggies at python.org
>> To change your subscription options or unsubscribe:
>> http://mail.python.org/mailman/listinfo/baypiggies
>
>

```