Dave Ja Vu
Dave Ja Vu

Reputation: 43

Python - Convert multiple lists to multiple dictionaries using one of lists as dict names

Working on a small python project and have reached a point that has got me stumped.

runners = ["Tom","Bob","Bill","Gary"]

runner_id = [123223,234345,356543,487334]

event_id = [11,12,13,14]

event_time = [12.30,13.00,14.00,16.00]

I have the data above and want to end up with a dictionary for each runner, with the dict named as the runners name.

So result would be as follows:

Tom = {runner_id : 123223, event_id : 11, event_time : 12.30}

Bob = {runner_id : 234345, event_id : 12, event_time : 13.00}

Bill = {runner_id : 356543, event_id : 13, event_time : 14.00}

Gary = {runner_id : 487334, event_id : 14, event_time : 16.00}

Stumped as to how to actually have the dict named as I need it to, ie as the names from one of the original lists.

Have read an awful lot of posts but can't find an answer to this specific question.

All helpful pointers in the right direction gratefully received.

Many thanks.

Upvotes: 0

Views: 109

Answers (4)

jcofta
jcofta

Reputation: 1

As follow

runners = ["Tom","Bob","Bill","Gary"]
runner_id = [123223,234345,356543,487334]
event_id = [11,12,13,14]
event_time = [12.30,13.00,14.00,16.00]

runners_dict = {}
for index, runner in enumerate(runners):
    runners_dict[runner] = {
        'runner_id' : runner_id[index],
        'event_id' : event_id[index],
        'event_time' : event_time[index]
    }

And if you want to get certain runner dict then

runners_dict['name']

Upvotes: 0

SuperBiasedMan
SuperBiasedMan

Reputation: 9969

It's a lot better if you instead have one dictionary containing all the runners and their dictionaries. So instead you have this:

runners = {
    "Tom" : {runner_id : 123223, event_id : 11, event_time : 12.30},
    "Bob" : {runner_id : 234345, event_id : 12, event_time : 13.00},
    "Bill" : {runner_id : 356543, event_id : 13, event_time : 14.00},
    "Gary" : {runner_id : 487334, event_id : 14, event_time : 16.00},
    }

Upvotes: 0

Kasravnd
Kasravnd

Reputation: 107287

You can use a dict comprehension and zip function :

runners = ["Tom","Bob","Bill","Gary"]
runner_id = [123223,234345,356543,487334]
event_id = [11,12,13,14]
event_time = [12.30,13.00,14.00,16.00]

print {name:{'runner_id' : i, 'event_id' : j, 'event_time' : k} for i,j,k,name in zip(runner_id,event_id,event_time,runners)}

result :

{'Bob': {'event_id': 12, 'event_time': 13.0, 'runner_id': 234345}, 'Bill': {'event_id': 13, 'event_time': 14.0, 'runner_id': 356543}, 'Gary': {'event_id': 14, 'event_time': 16.0, 'runner_id': 487334}, 'Tom': {'event_id': 11, 'event_time': 12.3, 'runner_id': 123223}}
[Finished in 0.1s]

Upvotes: 3

C.B.
C.B.

Reputation: 8326

You would have to use the runner names as keys to a larger dictionary

runner_dict = {}
for index, runner in enumerate(runners):
    runner_dict[runner] = {
        'runner_id'  : runner_id[index],
        'event_id'   : event_id[index],
        'event_time' : event_time[index]
    }

Upvotes: 1

Related Questions