'Remove list element based on part of a string
I have a long list json_response containing Twitter data. Some of the 293 elements in the list do not contain any tweets indicated by 'result_count': 0 and I want to delete those elements from json_response
The following should remove all elements containing 'result_count': 0. However, nothing happens when the code is executed
json_response = [element for element in json_response if element != "'result_count': 0"]
A sample of json_response where only the second out of four elements contain tweets.
print(json.dumps(json_response[0:4], indent=4, sort_keys=True))
[
{
"meta": {
"next_token": "b26v89c19zqg8o3fo77fw18ex7m9tkxtn5jx8qokz8y2l",
"result_count": 0
}
},
{
"data": [
{
"author_id": "751651375407181824",
"created_at": "2019-12-16T02:10:22.000Z",
"id": "1206396117425852417",
"text": "Tarkanian libel lawsuit against Jacky Rosen, 2016 opponent, blocked by Nevada Supreme Court"
},
{
"author_id": "7568942",
"created_at": "2019-12-15T04:41:00.000Z",
"id": "1206071638166507520",
"text": "Tarkanian libel lawsuit against Jacky Rosen, 2016 opponent, blocked by Nevada Supreme Court Dismissed thanks to NV's anti-SLAPP law"
},
{
"author_id": "2404787642",
"created_at": "2019-12-13T18:40:32.000Z",
"id": "1205558134317568000",
"text": "Tarkanian libel lawsuit against Jacky Rosen, 2016 opponent, blocked by Nevada Supreme Court"
},
{
"author_id": "245630545",
"created_at": "2019-12-13T18:06:29.000Z",
"id": "1205549565513883648",
"text": "Attacks lobbed in the heat of a campaign don't end with the campaign, Part 2: Supreme Court puts an end to Danny Tarkanian's libel lawsuit against Jacky Rosen for ads from a 2016 congressional campaign, also via @RileySnyder:"
},
{
"author_id": "56440142",
"created_at": "2019-12-12T22:26:06.000Z",
"id": "1205252514070839296",
"text": ".@DannyTarkanian libel lawsuit against @SenJackyRosen, 2016 opponent, blocked by Nevada Supreme Court via @RileySnyder\u200b"
},
{
"author_id": "794407888567476224",
"created_at": "2019-12-12T22:08:08.000Z",
"id": "1205247991029755905",
"text": "Tarkanian libel lawsuit against Jacky Rosen, 2016 opponent, blocked by Supreme Court\nVia @RileySnyder\n"
}
],
"includes": {
"users": [
{
"created_at": "2016-07-09T05:37:07.000Z",
"description": "Towanda! from Fried Green Tomatoes",
"id": "751651375407181824",
"name": "Karen Gruber",
"username": "mail4ufromme1"
},
{
"created_at": "2007-07-18T20:09:04.000Z",
"description": "Full-time software engineering manager, part-time educator, constant student, backpacker and disliker of the Oxford comma.",
"id": "7568942",
"name": "Justin Yost",
"username": "justinyost"
},
{
"created_at": "2014-03-22T17:05:36.000Z",
"description": "",
"id": "2404787642",
"name": "James Egan",
"username": "JamesEganLaw"
},
{
"created_at": "2011-02-01T03:39:40.000Z",
"description": "Assistant editor and reporter @TheNVIndy covering statehouse elections and more. Co-host of @nvindyespanol's Cafecito. Email me: [email protected]",
"id": "245630545",
"name": "Michelle Rindels",
"username": "MichelleRindels"
},
{
"created_at": "2009-07-13T17:49:46.000Z",
"description": "Curious about Congress and the beautiful game. Following the Nevada delegation for @TheNVIndy",
"id": "56440142",
"name": "Humberto Sanchez",
"username": "hsanchez128"
},
{
"created_at": "2016-11-04T05:16:14.000Z",
"description": "Nonprofit news outlet reporting on Nevada politics, policy and people since 2017 | Your State. Your News. Your Voice. | [email protected]",
"id": "794407888567476224",
"name": "Nevada Independent",
"username": "TheNVIndy"
}
]
},
"meta": {
"newest_id": "1206396117425852417",
"next_token": "b26v89c19zqg8o3fn0po9zgvw98j7w7sec5wgoh0s0rr1",
"oldest_id": "1205247991029755905",
"result_count": 6
}
},
{
"meta": {
"next_token": "b26v89c19zqg8o3fosns35qj7v5486697crmsdhl6kku5",
"result_count": 0
}
},
{
"meta": {
"next_token": "b26v89c19zqg8o3fo77h5ma6xw9tghoz8z8l6hgq0shod",
"result_count": 0
}
}
]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
