Skip to content
This repository was archived by the owner on Jul 7, 2023. It is now read-only.

Replace .append loop with list comprehension #1451

Merged
merged 1 commit into from
Mar 1, 2019

Conversation

lgeiger
Copy link
Contributor

@lgeiger lgeiger commented Feb 13, 2019

This PR replaces loops that only append to a list with list comprehensions. This makes it more concise and (arguably) more readably.

List comprehensions also are quite a bit faster than appending to a list. Toy example in Python 3.6:

In [1]: %%timeit
   ...: l = []
   ...: for i in range(5000):
   ...:     l.append(i)
375 µs ± 1.73 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [2]: %%timeit
   ...: l = [i for i in range(5000)]
168 µs ± 1.08 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)

@googlebot googlebot added the cla: yes PR author has signed CLA label Feb 13, 2019
@afrozenator
Copy link
Contributor

Hi @lgeiger -- Can you resolve the conflicts here? Sorry for the delay in merging.

This PR replaces loops that only append to a list with list comprehensions. This makes it more concise and arguably more readably.
List comprehensions also are quite a bit faster than appending to a list.
Toy example in Python 3.6:
```python
In [1]: %%timeit
   ...: l = []
   ...: for i in range(5000):
   ...:     l.append(i)
375 µs ± 1.73 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [2]: %%timeit
   ...: l = [i for i in range(5000)]
168 µs ± 1.08 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
```
@lgeiger lgeiger force-pushed the list-comprehensions branch from c523ef9 to 67fd40b Compare March 1, 2019 21:34
@lgeiger
Copy link
Contributor Author

lgeiger commented Mar 1, 2019

Rebased 👍

@afrozenator
Copy link
Contributor

Thanks @lgeiger !

@afrozenator afrozenator merged commit abd5ea2 into tensorflow:master Mar 1, 2019
@lgeiger lgeiger deleted the list-comprehensions branch March 1, 2019 23:06
tensorflow-copybara pushed a commit that referenced this pull request Mar 1, 2019
PiperOrigin-RevId: 236388504
kpe pushed a commit to kpe/tensor2tensor that referenced this pull request Mar 2, 2019
This PR replaces loops that only append to a list with list comprehensions. This makes it more concise and arguably more readably.
List comprehensions also are quite a bit faster than appending to a list.
Toy example in Python 3.6:
```python
In [1]: %%timeit
   ...: l = []
   ...: for i in range(5000):
   ...:     l.append(i)
375 µs ± 1.73 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [2]: %%timeit
   ...: l = [i for i in range(5000)]
168 µs ± 1.08 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
```
kpe pushed a commit to kpe/tensor2tensor that referenced this pull request Mar 2, 2019
PiperOrigin-RevId: 236388504
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
cla: yes PR author has signed CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants