Created
December 20, 2013 19:25
-
-
Save Kwpolska/8060014 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## go from here | |
mylist = [{'pk':1, 'f_id':5,'bool':True}, {'pk':2, 'f_id':2, 'bool':True}, {'pk':3, 'f_id':2, 'bool':False}, {'pk':4, 'f_id':7, 'bool':True}, {'pk':5,'f_id':2,'bool':True}, {'pk':6,'f_id':7, 'bool':False}] | |
## to here | |
# mynewlist = [{'pk':1,'f_id':5,'bool':True}, {'pk':5,'f_id':2,'bool':True}] | |
### filter out Falsey elements | |
mynewlist = [i for i in mylist if i['bool']] | |
### dropped Falsey element f_id’s | |
dropped = [i['f_id'] for i in mylist if not i['bool']] | |
dropped_twice = [] | |
print(dropped) | |
mynewnewlist = [] | |
for i in mynewlist: | |
if i['f_id'] not in dropped: | |
mynewnewlist.append(i) | |
elif i['f_id'] in dropped_twice: | |
mynewnewlist.append(i) | |
else: | |
dropped_twice.append(i['f_id']) | |
### create a dict of desired elements | |
desired = {} | |
for n, i in enumerate(mynewnewlist): | |
desired[i['f_id']] = n | |
### get the IDs out (py3k-compatible) | |
ids = sorted(list(desired.values())) | |
### create the new new list | |
mynewnewnewlist = [] | |
for i in ids: | |
mynewnewnewlist.append(mynewnewlist[i]) | |
print(mynewnewnewlist) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment