1

I am getting json response from SOLR via PySolr , everything works fine, just that Facet fields are coming in inconsistent format , below is the facet fields

LocalityId = [ "14008",1,"14293",4,]

Now I need to convert the above list into Key-Value pair (Dictionary), likewise

LocalityId = {"14008":"1", "14293":"4"}

How to achieve this with python ?

Edit : Yes I know its not standard list format , but I didnt created it , blame solr

Edit : Why -1 , stackoverflow has become a place for revenge and random outburst! tell me here folk where I am wrong ??

2
  • 1
    i didn't find a list like above. Mar 11, 2015 at 13:11
  • @AvinashRaj yep same here, but this the standard solr response with or without PySolr Mar 11, 2015 at 13:12

3 Answers 3

9

Assuming your original list is named fields:

dict(zip(fields[::2], map(str, fields[1::2])))

Step by step: From the original list fields we want the items at even indexes to be the keys and items at odd indexes to be the values.

We will use Python's extending indexing (which is actually the slice builtin under the hood) to obtain all even and odd indexed items:

>>> list(range(10)[::2])
[0, 2, 4, 6, 8]
>>> list(range(10)[1::2])
[1, 3, 5, 7, 9]

The even items are obtained by starting at index 0 (omitted since it's the default) up until the last element (omitted as well) and we will use a step of 2. For odd items we do the same but starting at index 1.

If we zip those two lists together we obtain a list of (key, value) that we can pass the return value to the dict constructor.

Now we only need to make the values strs to match the desired output, for that we will use map:

>>> even_items = fields[::2]
>>> odd_items = fields[1::2]
>>> key_value_pairs = zip(even_items, odd_items)
>>> dict(key_value_pairs)
{'14008': 1, '14293': 4}

Or in one line:

dict(zip(fields[::2], map(str, fields[1::2])))
4
  • 2
    Might add a map(str, ....) to the second part to exactly match the desired result.
    – tobias_k
    Mar 11, 2015 at 13:15
  • @A-NirVaan Bro, please add the exact input at very first. Mar 11, 2015 at 13:17
  • One quick question, how to do it for n number of elements ? loop ? Mar 11, 2015 at 13:22
  • 1
    Slicing works for list of any size range(100)[::2] will return every even number between 0 and 99. But maybe that's not what you're asking for?
    – El Bert
    Mar 11, 2015 at 13:24
2

You can use zip and dict for convert to your list to expected dictionary :

>>> l=[ "14008",1,"14293",4,]
>>> dict(zip(l[::2],l[1::2]))
{'14008': 1, '14293': 4}
0
1

Another way is to use dict comprehension.

l = ["14008", 1, "14293", 4]
{l[i*2]: l[i*2+1] for i in range(int(len(l)/2))}
1
  • Thanks Mika ji , really appreeciate Mar 11, 2015 at 13:46

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.