Anwar Noiine
Anwar Noiine

Reputation: 1

Nesting - list in dict and value from same index

I have a dictionary that contains about 470 key/value pairs where the value is a nested list. It looks like this:

{'sample1': [0.052333365, 0.048546686, 0.037446034, 0.034170007, 0.027255262, 0.014583427, 0.022703695, 0.02747237, 0.036779904, 0.047089636, 0.068311633, 0.131601903, 0.213325007, 0.262173714], 
'sample2': [0.499261188, 0.569225594, 0.971341941, 0.983553048, 1.047320154, 1.214003077, 1.382066271, 1.332965957, 1.353788699, 1.224310364, 1.27942017, 1.297752738, 1.215054777, 1.336544035], 
'sample3': [3.015670427, 3.608257648, 3.060244617, 2.879527679, 2.720453311, 2.889312783, 2.899236274, 2.762219639, 3.257009779, 3.113135178, 3.312874684, 3.328944661, 3.564360549, 3.480976541]}

It was originally created like this due to row based (sample) focus of the client who access the data. Each index is a measurement taken 2 months apart.

Recently that has changed to be more columnar in nature and I am getting questions about can be learned across all samples within a specific 2 month period.

My question is, is there an easy way to extract the value in say index [1] or [5] for all samples and still be able to identify which sample the values came from without having to recreate data into a two dimensional list?

Upvotes: 0

Views: 61

Answers (3)

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20490

We can use a dictionary comprehension to recreate a dictionary with only the data from specified indexes.

def get_data(dct, indexes):

    #Iterate through value list and collect element from specified indexes
    return {key: [value[idx] for idx in indexes] for key, value in dct.items()}

So for indexes [1,5], the output will be

print(get_data(dct, [1,5]))
#{'sample1': [0.048546686, 0.014583427], 'sample2': [0.569225594, 1.214003077], 'sample3': [3.608257648, 2.889312783]}

Upvotes: 0

Haleemur Ali
Haleemur Ali

Reputation: 28243

is there an easy way to extract the value in say index [1] or [5] for all samples and still be able to identify which sample the values came from without having to recreate data into a two dimensional list?

You could use a dictionary comprehension

Lets say your sample dictionary is stored in a variable called data and we want to extract the values in the 6th position

row_5 = {k: v[5] for k, v in data.items()} 
# row_5 prints
{'sample1': 0.014583427, 'sample2': 1.214003077, 'sample3': 2.889312783}

You can build a function to do this in a generic way.

def get_row(row_num, data_dict):
    return {k: v[row_num] for k, v in data_dict.items()}

If you want even more data processing functionality, I would suggest looking into the pandas library.

Upvotes: 1

Austin
Austin

Reputation: 26039

You can use dictionary items() and index values:

for k, v in d.items():
    print(f'{k} -> {v[1]}, {v[5]}')

where d is your dictionary.

For the given input, this outputs:

sample1 -> 0.048546685999999999, 0.014583426999999999
sample2 -> 0.56922559399999995, 1.2140030770000001
sample3 -> 3.6082576479999999, 2.8893127829999998

Upvotes: 1

Related Questions