I have a code as follows.
for item in my_list: print(item[0]) temp = [] current_index = my_list.index(item) garbage_list = creategarbageterms(item[0]) for ele in my_list: if my_list.index(ele) != current_index: for garbage_word in garbage_list: if garbage_word in ele: print("concepts: ", item, ele) temp.append(ele) print(temp) Now, I want to remove the ele from mylist when it gets appended to temp (so, that it won't get processed in the main loop, as it is a garbage word).
I know it is bad to remove elements straightly from the list, when it is in a loop. Thus, I am interested in knowing if there is any efficient way of doing this?
For example, if mylist is as follows;
mylist = [["tim_tam", 879.3000000000001], ["yummy_tim_tam", 315.0], ["pudding", 298.2], ["chocolate_pudding", 218.4], ["biscuits", 178.20000000000002], ["berry_tim_tam", 171.9], ["tiramusu", 158.4], ["ice_cream", 141.6], ["vanilla_ice_cream", 122.39999999999999]] 1st iteration
for the first element tim_tam, I get garbage words such as yummy_tim_tam and berry_tim_tam. So they will get added to my temp list.
Now I want to remove yummy_tim_tam and berry_tim_tam from the list (because they have already added to temp), so that it won't execute from the beginning.
2nd iteration
Now, since yummy_tim_tam is no longer in the list this will execute pudding. For pudding I get a diffrent set of garbage words such as chocolate_pudding, biscuits, tiramu. So, they will get added to temp and will get removed.
3rd iteration
ice_cream will be selected. and the process will go on.
My final objective is to get three separate lists as follows.
["tim_tam", 879.3000000000001], ["yummy_tim_tam", 315.0], ["berry_tim_tam", 171.9] , ["pudding", 298.2] ["chocolate_pudding", 218.4], ["biscuits", 178.20000000000002], ["tiramusu", 158.4] ["ice_cream", 141.6], ["vanilla_ice_cream", 122.39999999999999]
.indexing thing. It also seems thatgarbage_listshould really be asetinstead...["tim_tam", 879.3000000000001], ["yummy_tim_tam", 315.0], ["berry_tim_tam", 171.9],["pudding", 298.2], ["chocolate_pudding", 218.4], ["biscuits", 178.20000000000002], ["tiramusu", 158.4],["ice_cream", 141.6], ["vanilla_ice_cream", 122.39999999999999]. I will update the question again :)yunny_tim_tamandberry_tim_tamfromtemp? Or are their other words your trying to remove? Could you not just add everything and filter out what you don't want at the end of the loop? Or you could have some sort of pre-checking beforehand so you never add them to begin with.True,continueenumeratein the for loops to get the item index. Do not use 8 spaces for indent, just 4