Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - how to reduce a list and keep values?

I have a list looking like that:

[[12, 0.029], [12, 0.039], [12, 0.012], ...some hundreds more... [13, 0.04], [13, 0.01], ...]

The first values range from 3 to 15, with an overall count of about 3000 values

For boxplotting I would need a solution where a boxplot is created for each first value with all second values. Like:

data_to_plot = [ all second values of list with value 12], [all second values of list with value 13],... 

Looking like:

data_to_plot = [0.029, 0.039], [0.04, 0.01],...

Thanks!

like image 778
dh81 Avatar asked May 31 '26 19:05

dh81


2 Answers

It seems like you want a dictionary with the first values as keys and the second values as elements of a list. You could do something like:

data_dict = {}
for key, value in list:
    if key not in data_dict:
        data_dict.update({key: [value]})
    else:
        data_dict[key].append(value)

This will yield (using your example data) {12: [0.029, 0.039], 13: [0.04, 0.01]}

like image 154
Jeremy Weirich Avatar answered Jun 03 '26 14:06

Jeremy Weirich


Use a defaultdict with default as list so you don't need to check if a key already exists. Then collect values using the first items as keys:

from collections import defaultdict

result = defaultdict(list)

lst = [[12, 0.029], [12, 0.039], [13, 0.04], [13, 0.01]]
for l in lst:
    result[l[0]].append(l[1])

print(list(result.values()))
# [[0.029, 0.039], [0.04, 0.01]]

In this way, you still know which values belong to which keys.

data_to_plot = result.values()
keys_for_data = result.keys()
like image 39
Moses Koledoye Avatar answered Jun 03 '26 12:06

Moses Koledoye



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!