Question: I'm having trouble to define the `__delitem__` or similar method for the class `Container` in the example below. How can I rectify this? Thx.

```
import numpy as np
import pandas as pd

class XLData(object):
    def __init__(self, name):
        self.name = name
        self.data = pd.DataFrame({self.name: list("ASDF" * 2),
                                  'x': np.random.randint(1, 100, 8) })
    def __repr__(self):
        return repr(self.data.head(2))

class Container(object):
    def __init__(self):
        self.counter = 0
        self.items = []
    def append(self, item):
        self.counter += 1
        self.items = self.items + [item]
    def __delitem__(self, name):
        for c in self.items:
            print("element name:{}, to delete:{}".format(c.name, name))
            if c.name == name:
                pass #!
                #del c
    def __iter__(self):
        for c in self.items:
            yield c

a = XLData('a')
b = XLData('b')
c = XLData('c')

dl = Container()
dl.append(a)
dl.append(b)
dl.append(c)
del dl['b']

for c in dl:
    print(c)
# 'b' is still in ..

```

Answer: 
It is a good idea not to modify the array we are looping over in the loop itself. So just pick the index of the item and delete it outside the loop.

```
class Container(object):
    def __init__(self):
        self.counter = 0
        self.items = {}                # create a dict!
    def append(self, item):
        self.counter += 1
        self.items[item.name] = item   # add items to it, keyed under their names
    def __delitem__(self, name):
        del self.items[name]           # this becomes *really* simple, and efficient
    def __iter__(self):
        for c in self.items.values:    # loop over the dict's values to the items
            yield c #

```
It is a good idea not to modify the array we are looping over in the loop itself. So just pick the index of the item and delete it outside the loop.

```
def __delitem__(self, name):
    idx = -1
    found = False
    for c in self.items:
        idx += 1
        print("element name:{}, to delete:{}".format(c.name, name))
        if c.name == name:
            found = True
            break
    if found:
        del self.items[idx]

```