How to iterate over values ​​containing lists and delete items?

Beginner Python. I have a list dictionary, for example:

d = {
  1: ['foo', 'foo(1)', 'bar', 'bar(1)'],
  2: ['foobaz', 'foobaz(1)', 'apple', 'apple(1)'],
  3: ['oz', 'oz(1)', 'boo', 'boo(1)']
}

I am trying to figure out how to scroll through the dictionary keys and the corresponding list values ​​and delete all the lines in each of the list with tail brackets. So far this is what I have:

for key in keys:
    for word in d[key]...: # what else needs to go here?
        regex = re.compile('\w+\([0-9]\)')
        re.sub(regex, '', word)  # Should this be a ".pop()" from list instead?

I would like to do this with a list, but, as I said, I cannot find much information about the loop with the dict keys and the corresponding bits value in the list. What is the most efficient way to customize?

+1
source share
3 answers

You can rebuild the dictionary by allowing only elements without parentheses:

d = {k:[elem for elem in v if not elem.endswith(')')] for k,v in d.iteritems()}
+7
source
temp_dict = d
for key, value is temp_dict:
    for elem in value:
        if temp_dict[key][elem].find(")")!=-1:
            d[key].remove[elem]

, temp_list, , .

+1

Alternatively, you can do this without rebuilding the dictionary, which might be preferable if it is huge ...

for k, v in d.iteritems():
   d[k] = filter(lambda x: not x.endswith(')'), v)
+1
source

All Articles