Given is a list with unsorted indices in a list of length n. Each element of the list is only once contained. So the list looks like this
L = [13, 145, 70001, 34, ..., 533]
Also given is a dictionary d
which numerical values as key. All values are element
{0,1}
. Like
d = {
"[some data]" : 0,
"[some data]" : 1,
"[some data]" : 1,
"[some data]" : 1,
...
"[some data]" : 0
}
There are a lot more entries in the dictionary d
then in the list L
.
What I want to do is deleting data from the dictionary for each position (index) from L
if it is a 0
.
The problem that I see while proceeding it that after the each deletion the indices need to be shifted since the position within dictionary is changing. Which is quiet inefficient regarding on a large number of items in L
. There must be an efficient way of proceeding this task.
Any ideas and suggestions are highly appreciated!
Note that you should not expect to be able to do this as most dictionary implementations are not ordered, but Python's is since 3.6 and a part of the spec in 3.7 - but onto the question.
We can use a dictionary comprehension with enumerate
to make a new dictionary so we do not have to worry about the index shifting business that worries you.
L_ = set(L)
d = {k: v for i, (k, v) in enumerate(dict.items()) if i not in L_ and v}