HighwayJohn - 10 months ago 21

Python Question

I have lists of arbitrary size. This is an example:

`ax = ('0','1','1','1','2','2','2','3')`

For x axis labeling I want to convert this list to:

`ax = ('0','1','','','2','','','3')`

So duplicates should be erased while the list size should stay the same. Is their an easy way to do that?

Answer

```
In [12]: seen = set()
In [13]: [x if x not in seen and not seen.add(x) else '' for x in ax]
Out[13]: ['0', '1', '', '', '2', '', '', '3']
```

This is a slightly modified version of a uniquifier suggested by Dave Kirby, here.

`seen.add(x)`

adds `x`

to the set `seen`

. The `seen.add`

method returns `None`

. So
in a boolean context, (since `bool(None)`

is `False`

), `not seen.add(x)`

is always `True`

. Therefore the condition

```
x not in seen and not seen.add(x)
```

has a boolean value equal to

```
x not in seen and True
```

which is equivalent to

```
x not in seen
```

So the conditional expression

```
x if x not in seen and not seen.add(x) else ''
```

returns `x`

if `x`

is not already in `seen`

and returns `''`

if `x`

is already in `seen`

(and `x`

then gets added to `seen`

).