# How to create lists of cell neighbours?

**URL:** <https://fenicsproject.discourse.group/t/how-to-create-lists-of-cell-neighbours/4728>\
**Category:** Uncategorized\
**Created:** [December 14, 2020, 10:10am UTC](https://fenicsproject.discourse.group/t/how-to-create-lists-of-cell-neighbours/4728 "2020-12-14T10:10:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kris71272](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/kris71272/32/1974_2.png) [@kris71272](https://fenicsproject.discourse.group/u/kris71272)\
**Post date:** [December 14, 2020, 10:10am UTC](https://fenicsproject.discourse.group/t/how-to-create-lists-of-cell-neighbours/4728/1 "2020-12-14T10:10:49Z")

</div>

Hi,  
Is it possible to create a list for each cell, which contains the cells that share an edge with the original cell? Thus, to list the neighbours for each cell?

It seems to be answered here, but I cannot make it work, [https://fenicsproject.org/qa/3843/cell-neighbours/index.html](https://fenicsproject.org/qa/3843/cell-neighbours/index.html)

Thanks!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [December 14, 2020, 4:42pm UTC](https://fenicsproject.discourse.group/t/how-to-create-lists-of-cell-neighbours/4728/2 "2020-12-14T16:42:57Z")

</div>

Instead of using the filter function, you can simply unwrap it in multiple loops:

```auto
from dolfin import *

mesh = UnitSquareMesh(2, 2)

# Init facet-cell connectivity
tdim = mesh.topology().dim()
mesh.init(tdim - 1, tdim)

# For every cell, build a list of cells that are connected to its facets
# but are not the iterated cell
cell_neighbours = {}
for cell in cells(mesh):
    index = cell.index()
    cell_neighbours[index] = []
    for facet in facets(cell):
        facet_cells = facet.entities(tdim)
        for facet_cell in facet_cells:
            if (index!=facet_cell):
                cell_neighbours[index].append(facet_cell)
print(cell_neighbours)

```

---

<div class="post-metadata">

**Author:** ![bhaveshshrimali](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/bhaveshshrimali/32/124_2.png) [@bhaveshshrimali](https://fenicsproject.discourse.group/u/bhaveshshrimali)\
**Post date:** [December 14, 2020, 5:05pm UTC](https://fenicsproject.discourse.group/t/how-to-create-lists-of-cell-neighbours/4728/3 "2020-12-14T17:05:41Z")

</div>

`filter` returns a `filter` object. You can either pass it to a list constructor as

```python
cell_neighbors = {cell.index(): sum(( list(filter(lambda ci: ci != cell.index(),
                                            facet.entities(tdim)))
                                    for facet in facets(cell)), [])
                for cell in cells(mesh)}

```

or use a much more discernible approach as @dokken suggested above.

---

<div class="post-metadata">

**Author:** ![kris71272](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/kris71272/32/1974_2.png) [@kris71272](https://fenicsproject.discourse.group/u/kris71272)\
**Post date:** [December 15, 2020, 9:20am UTC](https://fenicsproject.discourse.group/t/how-to-create-lists-of-cell-neighbours/4728/4 "2020-12-15T09:20:41Z")

</div>

Thanks a lot for your help!
