Reputation: 1090
I have 2 lists containing JSON values, allBuilds
and rawRequirements
. Each list has a length of 1201 items.
The contents of allBuilds
looks like this:
{'id': 19553, 'buildTypeId': 'AlpsWeb_AlpsWebDeployWebDev'}
{'id': 24456, 'buildTypeId': 'AlpsWeb_AlpsWebDeployWebDevTomcat8', }
None
None
{'id': 19356, 'buildTypeId': 'AlpsWeb_AlpsWebDeployWebQa',}
{'id': 19357, 'buildTypeId': 'AlpsWeb_AlpsWebDeployWebQa',}
None
{'id': 19357, 'buildTypeId': 'AlpsWeb_AlpsWebDeployWebQa',}
I need to filter the contents of allBuilds
to remove None
while also removing the corresponding element from rawRequirements
. I tried
successfulBuilds = list(filter(None, allBuilds))
This removes None
but now the length of the new list successfulBuilds
is 972 while rawRequirements
is still 1201.
How can I filter allBuilds
for None
and also remove the corresponding list element in rawRequirements
?
---- EDIT ----
Here's an example of what I'm trying to do:
allBuilds | rawRequirements
id | Requirement value1
none | Requirement value2 <--
none | Requirement value3 <--
id | Requirement value4
none | Requirement value5 <--
id | Requirement value6
I need to remove the element in rawRequirements at the same column where "none" exists in allBuilds
Upvotes: 0
Views: 134
Reputation: 128
You can use zip:
zipped = zip(allBuilds,rawRequirements)
filtered_zip = ((el1, el2) for ab, rR in zipped if el1 != None))
filtered_allBuilds, filteredRequirements = zip(*filtered_zip)
Upvotes: 2
Reputation: 10957
I recommend using zip
:
allBuilds, rawRequirements = zip(*((ab, rR) for ab, rR in zip(allBuilds, rawRequirements) if ab is not None))
zip
takes some getting used to but it is very efficient for filtering or sorting multiple lists.
Upvotes: 3
Reputation: 11228
you can use this
a=[1,2,3,None,4,5,6,7,None,10]
b = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
new_a =[]
new_b =[]
for i, v in enumerate(a):
if v is not None:
new_a.append(v)
new_b.append(b[i])
print(new_a,new_b,sep='\n')
output
[1, 2, 3, 4, 5, 6, 7, 10] #a
[0, 1, 2, 4, 5, 6, 7, 9] #b
Upvotes: 2