# Initialize variables inline with ~\> such as \`0 ~\> total += value\`

**URL:** <https://discuss.python.org/t/initialize-variables-inline-with-such-as-0-total-value/72218>\
**Category:** Ideas\
**Created:** [November 24, 2024, 7:32pm UTC](https://discuss.python.org/t/initialize-variables-inline-with-such-as-0-total-value/72218 "2024-11-24T19:32:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![humblebumblebee](https://avatars.discourse-cdn.com/v4/letter/h/73ab20/32.png) [@humblebumblebee](https://discuss.python.org/u/humblebumblebee)\
**Post date:** [November 24, 2024, 7:32pm UTC](https://discuss.python.org/t/initialize-variables-inline-with-such-as-0-total-value/72218/1 "2024-11-24T19:32:04Z")

</div>

Add a shorthand notation for initializing a variable in place such as

Example 1:

```python
#Current
total = 0
for i in range(0,10):
    total += i

```

```python
#Proposed
for i in range(0,10):
    0 ~> total += i

```

Example 2:

```python
#Current
items = []
for num in numbers:
    if num > 10:
        items.append(num)

```

```python
#Proposed
for num in numbers:
    if num > 10:
        [] ~> items.append(num)

```

Example 3:

```python
#Current
sale = {}
for p, q in zip(products, quantities):
    if p in sale:
        sale[p] += q
    else:
        sale[p] = q

```

```python
#Proposed
sale = {}
for p, q in zip(products, quantities):
    0 ~> sale[p] += q

```

---

<div class="post-metadata">

**Author:** ![effigies](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/effigies/32/35835_2.png) [@effigies](https://discuss.python.org/u/effigies)\
**Post date:** [November 24, 2024, 7:39pm UTC](https://discuss.python.org/t/initialize-variables-inline-with-such-as-0-total-value/72218/2 "2024-11-24T19:39:34Z")

</div>

This doesn’t allow you to do anything new, it just saves you a few lines at the cost of new syntax. Explicit variable initialization is not a problem to be solved, IMO.

As to your last example:

```python
from collections import defaultdict

sale = defaultdict(lambda: 0)
for p, q in zip(products, quantities):
    sale[p] += q

```

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [November 24, 2024, 7:40pm UTC](https://discuss.python.org/t/initialize-variables-inline-with-such-as-0-total-value/72218/3 "2024-11-24T19:40:10Z")

</div>

or just `defaultdict(int)`

---

<div class="post-metadata">

**Author:** ![effigies](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/effigies/32/35835_2.png) [@effigies](https://discuss.python.org/u/effigies)\
**Post date:** [November 24, 2024, 7:40pm UTC](https://discuss.python.org/t/initialize-variables-inline-with-such-as-0-total-value/72218/4 "2024-11-24T19:40:52Z")

</div>

Shows you how often I actually reach for `defaultdict`…

---

<div class="post-metadata">

**Author:** ![humblebumblebee](https://avatars.discourse-cdn.com/v4/letter/h/73ab20/32.png) [@humblebumblebee](https://discuss.python.org/u/humblebumblebee)\
**Post date:** [November 24, 2024, 7:42pm UTC](https://discuss.python.org/t/initialize-variables-inline-with-such-as-0-total-value/72218/5 "2024-11-24T19:42:15Z")

</div>

Yes, defaultdict is one way for dicts. I think to have a generic syntax that covers every single variable type is still valuable, since you don’t need to remember how to handle each variable type.

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [November 24, 2024, 7:44pm UTC](https://discuss.python.org/t/initialize-variables-inline-with-such-as-0-total-value/72218/6 "2024-11-24T19:44:55Z")

</div>

Yeah we have that, it’s an assignment statement. It’s not worth the complexity and added syntax to save one line of code.

---

<div class="post-metadata">

**Author:** ![Rosuav](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rosuav/32/3429_2.png) [@Rosuav](https://discuss.python.org/u/Rosuav)\
**Post date:** [November 24, 2024, 8:20pm UTC](https://discuss.python.org/t/initialize-variables-inline-with-such-as-0-total-value/72218/7 "2024-11-24T20:20:17Z")

</div>

> [@humblebumblebee](#):
>
> ```python
> #Current
> total = 0
> for i in range(0,10):
> total += i
> 
> ```
> 
> ```python
> #Proposed
> for i in range(0,10):
> 0 ~> total += i
> 
> ```

This is a bug magnet. Consider this (rather silly) example:

```python
stuff = [1, 4, 2, 8, 5, 7]
for num in stuff:
    total = 0
    for i in range(num):
        total += num * i
    print(num, total)

```

This is perfectly reasonable and easy to wrap your head around. Try defining reasonable semantics for the “initialize” semantics that won’t surprise people.

---

<div class="post-metadata">

**Author:** ![bulletmark](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bulletmark/32/12863_2.png) [@bulletmark](https://discuss.python.org/u/bulletmark)\
**Post date:** [November 24, 2024, 9:52pm UTC](https://discuss.python.org/t/initialize-variables-inline-with-such-as-0-total-value/72218/8 "2024-11-24T21:52:21Z")

</div>

> [@jamestwebber](#):
>
> or just `defaultdict(int)`

or just `Counter()`

---

<div class="post-metadata">

**Author:** ![jamestwebber](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jamestwebber/32/12799_2.png) [@jamestwebber](https://discuss.python.org/u/jamestwebber)\
**Post date:** [November 24, 2024, 9:58pm UTC](https://discuss.python.org/t/initialize-variables-inline-with-such-as-0-total-value/72218/9 "2024-11-24T21:58:54Z")

</div>

aw dangit 😂

---

<div class="post-metadata">

**Author:** ![bulletmark](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bulletmark/32/12863_2.png) [@bulletmark](https://discuss.python.org/u/bulletmark)\
**Post date:** [November 24, 2024, 10:13pm UTC](https://discuss.python.org/t/initialize-variables-inline-with-such-as-0-total-value/72218/10 "2024-11-24T22:13:32Z")

</div>

Note for this use case I generally opt for `defaultdict(int)` over `Counter()` because it is very slightly more performant. Just wanted to point it out though because `Counter()` is perhaps easier to remember.

---

<div class="post-metadata">

**Author:** ![hauntsaninja](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/hauntsaninja/32/12451_2.png) [@hauntsaninja](https://discuss.python.org/u/hauntsaninja)\
**Post date:** [November 25, 2024, 6:17am UTC](https://discuss.python.org/t/initialize-variables-inline-with-such-as-0-total-value/72218/11 "2024-11-25T06:17:10Z")

</div>

If you must use a normal dict for whatever reason, you can also do `sale.setdefault(p, 0)`

(and in the case the value is mutable you can even fit it all on one line, e.g. `d.setdefault(k, []).append(v)`)
