0

I have this kind of JSON data and have around 100 questions. How can I filter the question only up to 40?

This is the sample of data:

data=[{"id": "AA11",
"resp": [
    {"answer": "A","number": 1},
    {"answer": "A","number": 2},
    {"answer": "B","number": 3},
    {"answer": "D","number": 4},
    {"answer": "E","number": 5}
]},
{"id": "AA22",
"resp": [
    {"answer": "A","number": 1},
    {"answer": "A","number": 2},
    {"answer": "B","number": 3},
    {"answer": "D","number": 4},
    {"answer": "E","number": 5}
]},
{"id": "AA33",
"resp": [
    {"answer": "A","number": 1},
    {"answer": "A","number": 2},
    {"answer": "B","number": 3},
    {"answer": "D","number": 4},
    {"answer": "E","number": 5}
]}]

Can I extract the data only up to number 3 as example in this case?

Output:

data=[{"id": "AA11",
"resp": [
    {"answer": "A","number": 1},
    {"answer": "A","number": 2},
    {"answer": "B","number": 3}
]},
{"id": "AA22",
"resp": [
    {"answer": "A","number": 1},
    {"answer": "A","number": 2},
    {"answer": "B","number": 3}
]},
{"id": "AA33",
"resp": [
    {"answer": "A","number": 1},
    {"answer": "A","number": 2},
    {"answer": "B","number": 3}
]}]
Prashant Kumar
  • 2,057
  • 2
  • 9
  • 22
FrahChan04
  • 178
  • 1
  • 11
  • Does this answer your question? [How to take the first N items from a generator or list in Python?](https://stackoverflow.com/questions/5234090/how-to-take-the-first-n-items-from-a-generator-or-list-in-python) – Masklinn Apr 02 '20 at 07:04

4 Answers4

2
for item in data:
    item['resp'] = item['resp'][:3]
2

The Pythonic way would be a comprehension:

filtered = [{'id': d['id'], 'resp': [x for x in d['resp'] if x['number'] <= 3]}
            for d in data]

It gives as expected:

[{'id': 'AA11',
  'resp': [{'answer': 'A', 'number': 1},
           {'answer': 'A', 'number': 2},
           {'answer': 'B', 'number': 3}]},
 {'id': 'AA22',
  'resp': [{'answer': 'A', 'number': 1},
           {'answer': 'A', 'number': 2},
           {'answer': 'B', 'number': 3}]},
 {'id': 'AA33',
  'resp': [{'answer': 'A', 'number': 1},
           {'answer': 'A', 'number': 2},
           {'answer': 'B', 'number': 3}]}]
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • 1
    "...Complex is better than complicated. Flat is better than nested. Sparse is better than dense. Readability counts..." -The Zen of Python – Shane Abram Mendez Apr 02 '20 at 07:19
1

Try this :

new_data = data[0:40]
for item in new_data:
    item['resp'] = item['resp'][0:3]

This solution will create a new dictionary with expected values.

Prashant Kumar
  • 2,057
  • 2
  • 9
  • 22
1

Filtering up to a specific element should can be done like this

def filter(question,num):
    temp = {}
    for key in question:
        temp[key] = question[key]
    temp['resp'] = question['resp'][:num]
    return temp

def filter_list(questions,num):
    temp = []
    for question in questions:
        temp.append(filter(question,num))
    return temp

data = [{}...]
filtered_data = filter_list(data)


You'll notice that filter creates a copy of question and only changes the copy. It can be useful to change data structures, but it's good practice to treat data structures as immutable where possible.

As a bonus, you won't lose any of the original data this way