1

I have a very large list alist which has a dict and a nested list like this:

a_list = [{'A': [], 's': {'code': '0', 'credits': '0', 'msg': 'OK'}},
 {'A': [{'dictionary': 'True',
    'item': 'pineapples',
    'id': '13',
    'score': '9.7899',
    'rollup': {'True': 'OK', 'Fiz': 'Yes'},
    'variant_list': [{'endp': '8', 'form': 'pineapple', 'register': '0'}]}], 'status': {'codecheck': '0', 'cred': '90809890', 'msg': 'OK'}},
......

{'A': [], 's': {'code': '0', 'credits': '0', 'msg': 'OK'}},
    ]

How can I extract the extract the item parameter if and only if exist into a list like this:

['NaN', 'pineapples', 'NaN']

I do not understand how to parse it since it has a very nested structure, the main issue which I am struggling with is to accessing to each element of the list and then to the other list and leaving a NaN string.

4
  • what doesn mean and leaving a NaN string ? Commented Jan 19, 2017 at 19:26
  • what do you mean by if it exists in a string? Commented Jan 19, 2017 at 19:27
  • @RomanPerekhrest thanks for the help!, as you can see in the first and the third element of a_list they have just an empty list {'A': [], 's': {'code': '0', 'credits': '0', 'msg': 'OK'}} I would like to impute a NaN there, since it has a void list. Commented Jan 19, 2017 at 19:28
  • @tooty44, yes for instance just replace this {'A': [], 's': {'code': '0', 'credits': '0', 'msg': 'OK'}}, for NaN Commented Jan 19, 2017 at 19:29

1 Answer 1

1

Use the following approach(list comprehension):

a_list = [{'A': [], 's': {'code': '0', 'credits': '0', 'msg': 'OK'}},
          {'A': [{'dictionary': 'True',
                  'item': 'pineapples',
                  'id': '13',
                  'score': '9.7899',
                  'rollup': {'True': 'OK', 'Fiz': 'Yes'},
                  'variant_list': [{'endp': '8', 'form': 'pineapple', 'register': '0'}]}],
           'status': {'codecheck': '0', 'cred': '90809890', 'msg': 'OK'}},
          {'A': [], 's': {'code': '0', 'credits': '0', 'msg': 'OK'}},
          ]

result = ['NaN' if not len(o['A']) else o['A'][0]['item'] for o in a_list]
print(result)

The output:

['NaN', 'pineapples', 'NaN']

List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition.

a certain condition, in your case, is 'NaN' if not len(o['A']) else o['A'][0]['item']

https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

Sign up to request clarification or add additional context in comments.

1 Comment

Could you provide some explanation?.. I didnot understood pretty well the list comprehension.

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.