-3

I have a list like

a=[('policy', 871),
 ('insurance', 382),
 ('life', 357),
 ('request', 270),
 ('call', 260)]

Now, I want to convert it to a list of one item dict:

[{'call': 260},{'insurance': 382},{'life': 357}, {'policy': 871}, {'request': 270}]
Iron Fist
  • 10,739
  • 2
  • 18
  • 34
Chandan
  • 752
  • 6
  • 12
  • Are we downvoting for questions that people (themselves) consider obvious these days? This question is concise, contains code (albeit minimal) and the expected result. Part of me wishes more questions were like this. Certainly we have better targets to throw our downvotes at. – jedwards Jul 20 '16 at 08:08
  • 1
    _Why_ do you want a list of 1 item dictionaries rather than a single flat dictionary? – PM 2Ring Jul 20 '16 at 08:22
  • 1
    @jedwards I can see sample input & expected output, admittedly in the form of Python data structures, but there is no code that actually attempts to produce that output from the supplied input. – PM 2Ring Jul 20 '16 at 08:30
  • @PM2Ring Right, and usually I'd prefer to see attempts also, moreso for complicated questions ("I don't want to do your work for you" type of thing), but can you imagine the crazy code someone might come up with to try to build a JSON manually. I'd be no better able to answer the user's question if they included code showing their attempt at forming the dictionary iteratively and building a JSON "by hand". This is not true in most cases, usually even a failing attempt sheds some additional light on what they're trying to accomplish. Here, I don't think it would. – jedwards Jul 20 '16 at 08:34
  • @jedwards I think, the point of failed attempt is to show that the OP has done at least some research, spent some time on the problem and doesn't treat this site as a free code service. This question shows no research attempt and so, should be closed. I would rather see a nightmare code which builds json manually than just empty "I have this, I want this." – khajvah Jul 20 '16 at 08:53
  • @khajvah I think this is your misconception that without any attempt the question has been posted here. We are all mostly professional, so we understand the value of our 'time'. It is not possible always to post the attempts that have been made to solve the problem. – Chandan Jul 20 '16 at 09:28
  • 1
    @user2934968 Yeah the problem is there is no way for us to know you are a professional or just posting a high school homework. Why is it not possible to show your attempts btw? – khajvah Jul 20 '16 at 09:37
  • @khajvah [Not all questions benefit from including code](http://stackoverflow.com/help/how-to-ask). Indeed, in this case, it would certainly serve to detract from the question, adding things to read that were unnecessary to understand the question. If you want to take the stance of only helping those who post their attempts, I won't try to talk you out of it. In fact, I if the question were even a little more complex, I too would like to see an attempt. But some of the highest rated questions under the python tag are simply stated and, in fact, *codeless*. – jedwards Jul 20 '16 at 09:47
  • 1
    @jedwards Indeed, "theoretical" questions that ask for an explanation and are not about specific code don't need to include code. High rated questions like "What does the yield keyword do in Python?" are fine as it's not about a specific case and thus, cannot include any code. Another kind are the questions that are so general that help many people([here is an example](https://stackoverflow.com/questions/53513/best-way-to-check-if-a-list-is-empty)). This is non of those cases though. This is a dry demand for a specific code that would help nobody but the OP(at least with that title). – khajvah Jul 20 '16 at 10:04

3 Answers3

6

You can convert a list of tuples to a dict simply by converting it to a dict:

>>> a = [('policy', 871), ('insurance', 382), ('life', 357), ('request', 270), ('call', 260)]
>>> dict(a)

will result in

{'policy': 871, 'call': 260, 'life': 357, 'request': 270, 'insurance': 382}

If you want it as a json string, try this:

>>> import json
>>> json.dumps(dict(a))

this time it will print a json formatted string instead of a dict (note the enclosing quotes):

'{"policy": 871, "call": 260, "life": 357, "request": 270, "insurance": 382}'

Update: If you need a list of dicts instead, try the following method:

>>> map(lambda x: {x[0]: x[1]}, a)
[{'policy': 871}, {'insurance': 382}, {'life': 357}, {'request': 270}, {'call': 260}]
Selcuk
  • 57,004
  • 12
  • 102
  • 110
  • 1
    this result is not as per my requirement – Chandan Jul 20 '16 at 08:13
  • 1
    @user2934968 I think you should modify your question then, because your question specifically mentions json and this answer is the correct way to **"convert a list into a json structure"**. – ChaoticTwist Jul 20 '16 at 08:33
4

I think you wanted a list of dictionaries, so here is one way:

>>> [{k:v} for (k,v) in a]
[{'policy': 871}, {'insurance': 382}, {'life': 357}, {'request': 270}, {'call': 260}]
Iron Fist
  • 10,739
  • 2
  • 18
  • 34
0

My version

def convert_to_dict(a):
    return [{i[0]: i[1]} for i in a]

Hope this helps :)

Anivarth
  • 619
  • 5
  • 19