0

I have this array of JSON:

[{'pk': 4L, 'model': u'logic.member', 'fields': {'profile': 3L, 'name': u'1', 'title': u'Mr', 'dob': datetime.date(1983, 1, 1), 'lastname': u'jkjk', 'redressno': u'jdsfsfkj', 'gender': u'm'}}, {'pk': 5L, 'model': u'logic.member', 'fields': {'profile': 3L, 'name': u'2', 'title': u'Mr', 'dob': datetime.date(1983, 1, 1), 'lastname': u'jkjk', 'redressno': u'jdsfsfkj', 'gender': u'm'}}]

I want to make separate array of JSON for only fields property.

What I tried is:

memarr=[] 

       for index,a in data1:
          print index
          print a
          memarr[index]=a[index].fields

And it is giving an error of:

too many values to unpack

Please correct.

2 Answers 2

2

First of all, data1 is a list, so you can't unpack it into 2 variables.

If you want the index, you have to use something like enumerate.

Second, you can't assign to a list via indexing if the key doesn't exist. You have to append or use another valid list insert method.

Third, a[index].fields doesn't really make sense - there is no key in a that would be associated with an integer index and fields is not an attribute.

You're probably looking for something like this:

memarr = []
for index, a in enumerate(data1):
    memarr.append(a['fields'])
Sign up to request clarification or add additional context in comments.

Comments

0

So many things wrong with that snippet...

Anyway:

memarr = [a['fields'] for a in data]

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.