Project Holy_Python_Grail_Quest - To insert multiple values in multiple keys of an Ordered Dictionary

So, I have been progressing on my Pythonic Quest. On this quest, I have learnt and found about many libraries and functions. Also, I have come across some interesting problems like the one mentioned in the title.

It serves as a temporary stop gap in my application until I implement sql or something better in my application.

So, my requirement is an ordered dictionary having multiple keys with multiple values. This ordered dictionary acts an SQL table having primary key. I hope you get an idea about what I'm talking about.

So, given below is the way I implemented it:

 from collections import OrderedDict

 #First initialize OrderedDict using its constructor
 a = OrderedDict({'name':['James','Bond'], 'id':[101,102']})

#Define keys
key1 = "name"
a.setdefault(key1,[])
a[key1].append( 'Max')

key2 = "id"
a.setdefault(key2,[])
a[key2].append(103)

Output will be:

OrderedDict ( [ ( 'id', [101, 102, 103] ) , ('name', ['James, ' Bond', 'Max'] ) ] )

Mission Accomplished.
Hope it will become useful for others also.

Ciao.
And Happy Holi to all.
MG





Comments

Popular posts from this blog

Project Wild Penguin 12.4.2017: Installing Broadcom Wireless Drivers to connect to JBL Bluetooth Speaker on Xubuntu 16.10

Project Wild Penguin 22.4.2017: To delete directories inside current directory whose contents are less than a given size

ANPR Project Day 1: Standard Format of Indian Number Plate