I am using Python 3.8 or 3.9.
I have a list of lists. In reality it will be 550K items long, and the inner lists bigger and more complex, but as a simplified example:
big_list = [["a",9,"abc"],["c", 21, "xzy"],["h", 28, "ght"],["d",200,"mfp"],["g",19,"cyp"]]
I want to remove a single inner list from the big_list based on the value of an item in an inner list. These values will be unique and exist in only one inner list.
Since I will have 20 or more threaded processes working with list items, I can't just work on the last item of the big_list and pop it when done.
What is the most efficient / effective approach to do this
big_list.pop(n)
where n is the index of the sublist containing, say, "ght" as it's third element?
This works, but is it the best approach?
big_list = [["a",9,"abc"],["c", 21, "xzy"],["h", 28, "ght"],["d",200,"mfp"],["g",19,"cyp"]]
key = "ght"
print(big_list)
count = 0
for item in big_list:
if item[2] == "ght":
big_list.pop(count)
break
count = 1
print(big_list)
Or, is this fine and I'm overthinking it? :)
CodePudding user response:
If you are not using the item you're popping, don't use list.pop
. Mutating a list you're iterating over is going to be a source of bugs and pain in the future.
It would be simpler and more efficient to just filter the list for the elements you want to keep rather than pop the ones you don't want.
filtered = [item for item in big_list if item[2] != "ght"]
Alternatively with filter
,
filtered = list(filter(lambda item: item[2] != "ght", big_list))
CodePudding user response:
Not a big deal, but using enumerate
instead of updating a variable yourself will give you a 20-25% speed-up