Reputation: 126
My program has many lists that are dependent on each other. What I need is when I remove an element from the main list (called LIST_5) by index, it will remove the corresponding index on all other lists.
I'm stuck after deleting only the elements from the main list (LIST_5), and I have no idea how to do the same for the other lists.
LIST_1 = ['english', 'english', 'french', 'english', 'english', 'english']
LIST_2 = ['documentation', 'music', 'position', 'social media', 'microeconomics', 'financial equations']
LIST_3 = ['ea_12', 'ea_12', 'ea_12', 'ea_12', 'ea_12', 'ea_12']
LIST_4 = ['141031', '141032', '141033', '141034', '141035', '141036']
LIST_5 = ['allowed', 'allowed', 'rejected', 'allowed', 'rejected', 'allowed']
for L5 in LIST_5:
if 'allowed' in L5:
pass
else:
LIST_5.remove(L5)
print(LIST_5)
# OUTPUT:
# ['allowed', 'allowed', 'allowed', 'allowed']
# How to apply the removal process for all other lists?
So the output of that would be:
# LIST_1 = ['english', 'english', 'english', 'english']
# LIST_2 = ['documentation', 'music', 'social media', 'financial equations']
# LIST_3 = ['ea_12', 'ea_12', 'ea_12', 'ea_12']
# LIST_4 = ['141031', '141032', '141034', '141036']
# LIST_5 = ['allowed', 'allowed', 'allowed', 'allowed']
Upvotes: 0
Views: 67
Reputation: 36
You can do it like this, it's not beautifull but it works:
for i in range(len(LIST_5)-1, -1, -1):
if 'allowed' in LIST_5[i]:
pass
else:
LIST_1.pop(i)
LIST_2.pop(i)
LIST_3.pop(i)
LIST_4.pop(i)
LIST_5.pop(i)
Pop deletes element on index, we go in reverse so we don't mess up our indexes.
Edit: The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.
So if we look at range function as range(start, stop, step), my range specifies to go from len(LIST_5)-1 (in this case 5) to 0 (we set stop as -1 because it will stop one step before it) by step -1, so it goes in reverse. You can read more here: https://realpython.com/python-range/
As to why we are doing it in reverse? If we did it from 0 upwards and deleted elements at index 2 our lists would be shorter, so our iteration would go out of bounds at some point. By doing it in reverse we avoid this issue, as indexes that we need to check don't change.
Upvotes: 1
Reputation: 16147
You want to zip them in a way that each word gets paired with the other words from the same index in each other list. zip
does that and you can used index 4 for each new list to keep or drop. Then it's a matter of reshaping them back to the original form with list(zip(*...)))
LIST_1 = ['english', 'english', 'french', 'english', 'english', 'english']
LIST_2 = ['documentation', 'music', 'position', 'social media', 'microeconomics', 'financial equations']
LIST_3 = ['ea_12', 'ea_12', 'ea_12', 'ea_12', 'ea_12', 'ea_12']
LIST_4 = ['141031', '141032', '141033', '141034', '141035', '141036']
LIST_5 = ['allowed', 'allowed', 'rejected', 'allowed', 'rejected', 'allowed']
Edit:
To explain what's happening here, consider the inside zip
tuple(zip(LIST_1,LIST_2,LIST_3,LIST_4,LIST_5))
It's reshaping the data column-wise
(('english', 'documentation', 'ea_12', '141031', 'allowed'),
('english', 'music', 'ea_12', '141032', 'allowed'),
('french', 'position', 'ea_12', '141033', 'rejected'),
('english', 'social media', 'ea_12', '141034', 'allowed'),
('english', 'microeconomics', 'ea_12', '141035', 'rejected'),
('english', 'financial equations', 'ea_12', '141036', 'allowed'))
Here the last value in each group is at index 4, and is allowed/rejected, so in the list comprehension we keep only elements where the word at index 4 is 'allowed'
You can see that behavior here where what you get is a list of columns that pass the conditions:
[x for x in tuple(zip(LIST_1,LIST_2,LIST_3,LIST_4,LIST_5)) if x[4]=='allowed']
Output
[('english', 'documentation', 'ea_12', '141031', 'allowed'),
('english', 'music', 'ea_12', '141032', 'allowed'),
('english', 'social media', 'ea_12', '141034', 'allowed'),
('english', 'financial equations', 'ea_12', '141036', 'allowed')]
Now we can use *
to unpack the list of tuples, and zip
to change them back to row-wise, basically reversing the first zip
. Lastly you can assign them back to their original values.
The entire code.
LIST_1, LIST_2, LIST_3, LIST_4, LIST_5 = list(zip(*[x for x in tuple(zip(LIST_1,LIST_2,LIST_3,LIST_4,LIST_5)) if x[4]=='allowed']))
Upvotes: 2