1
  1. How can we convert a dict with tuples (key, value), into a list of dicts, each dict with keys "key" and "value"?

    For example:

    dic={'Tim':3, 'Kate':2}
    

    becomes

    lst = [{'Name':'Tim', 'Age':3}, {'Name':'Kate', 'Age':2}]
    
  2. What are some advantages and disadvantages of using each representation? (e.g. consider the operations which we can think of and apply to them naturally). Thanks.
Tim
  • 1
  • 141
  • 372
  • 590
  • 1
    in a dict key look up is O(1) ... in a list you are looking at O(N) to find a given entry – Joran Beasley Mar 25 '15 at 19:34
  • Didn't you ask this question like a week ago? – Shashank Mar 25 '15 at 19:35
  • Are they the same? @Shash? – Tim Mar 25 '15 at 19:37
  • @Tim Hmm...I guess not. I was just having deja vu. But anyways, I believe this link will help you a lot in determining which data structure to use and when: https://wiki.python.org/moin/TimeComplexity :) – Shashank Mar 25 '15 at 19:39
  • @joran: Thanks. Nice to know it. By the way, ,my purpose is to [print the original dictionary into a table](http://stackoverflow.com/questions/29265002/print-a-dictionary-into-a-table). – Tim Mar 25 '15 at 19:42

3 Answers3

5

You can use a list comprehension :

>>> dic={'Tim':3, 'Kate':2}
>>> [{'Name':i, 'Age':j} for i,j in dic.items()]
[{'Age': 3, 'Name': 'Tim'}, {'Age': 2, 'Name': 'Kate'}]

and for opposite :

>>> l=[{'Name':i, 'Age':j} for i,j in dic.items()]
>>> dict((i.values()[::-1] for i in l))
{'Tim': 3, 'Kate': 2}
Mazdak
  • 105,000
  • 18
  • 159
  • 188
  • Thanks. Do you know how to do the opposite: from a list of dictionaries with same keys, to a dictionary? – Tim Mar 25 '15 at 19:40
  • that reversal function is dangerous as dicts do not have order you are not guaranteed the right order ... – Joran Beasley Mar 25 '15 at 21:56
4

Dictionary to list of dictionary

>>> dic={'Tim':3, 'Kate':2}
>>> [{"Name":i, "Age":j} for i,j in dic.items()]
[{'Age': 3, 'Name': 'Tim'}, {'Age': 2, 'Name': 'Kate'}]
>>> 

Best to use Dictionary Data Structure because Time Complexity to Find Key is O(1) Constant Time i.e. it is not depend on the size of dictionary

Demo to find age of Kate.

Need to Iterate every element from the List. Time Complexity is from O(1) to O(N)

>>> info
[{'Age': 3, 'Name': 'Tim'}, {'Age': 2, 'Name': 'Kate'}]
>>> age = -1
>>> find_name = "Kate"
>>> for i in info:
...    if i["Name"]==find_name:
...      age = i["Age"]
...      break
... 
>>> age
2
>>> 

By dictionary: Time Complexity is O(1)

>>> dic = {'Tim':3, 'Kate':2}
>>> find_name = "Kate"
>>> if find_name in dic:
...    age = dic[find_name]
... else:
...    age = -1
...    print "No data for %s name"%find_name
... 
>>> age
2
>>> 
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56
3

Another way using map

>>> dic={'Tim':3, 'Kate':2}
>>> map(lambda x: x,dic)
['Tim', 'Kate']
>>> map(lambda x:{'Age':dic[x],'Name':x},dic)
[{'Age': 3, 'Name': 'Tim'}, {'Age': 2, 'Name': 'Kate'}]
>>> 

Note - map is slower compared to a list comprehension, but I have just added it as an alternative

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140