# List items to different variables

**URL:** <https://discuss.python.org/t/list-items-to-different-variables/16605>\
**Category:** Python Help\
**Created:** [June 17, 2022, 8:07pm UTC](https://discuss.python.org/t/list-items-to-different-variables/16605 "2022-06-17T20:07:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jannehof](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jannehof/32/7646_2.png) [@jannehof](https://discuss.python.org/u/jannehof)\
**Post date:** [June 17, 2022, 8:07pm UTC](https://discuss.python.org/t/list-items-to-different-variables/16605/1 "2022-06-17T20:07:37Z")

</div>

I get a list that is of different length, in example below, 4 or 2 items, I want to save in different variables for later handling. (I use other objects. Both snip below solve the problem, but is there a more pythonic way to do this?

```auto
my_list = [4,6,2,11]
my_list = [7,19]

# sample 1

try:
   i1 = my_list[0]
   i2 = my_list[1]
   i3 = my_list[2]
   i4 = my_list[3]
except:
   pass

```

```auto
# sample 2

list_len = len(my_list)

if list_len > 0: i1 = my_list[0]
if list_len > 1: i2 = my_list[1]
if list_len > 2: i3 = my_list[2]
if list_len > 3: i4 = my_list[3]

```

```auto
# sample 3

# how do it nice in python?

```

---

<div class="post-metadata">

**Author:** ![BowlOfRed](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bowlofred/32/3379_2.png) [@BowlOfRed](https://discuss.python.org/u/BowlOfRed)\
**Post date:** [June 17, 2022, 8:53pm UTC](https://discuss.python.org/t/list-items-to-different-variables/16605/2 "2022-06-17T20:53:34Z")

</div>

What’s the advantage of using `i3` over `my_list[2]`?

My recommendation would be to not do that in the first place and just use the index.

---

<div class="post-metadata">

**Author:** ![jannehof](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/jannehof/32/7646_2.png) [@jannehof](https://discuss.python.org/u/jannehof)\
**Post date:** [June 17, 2022, 8:59pm UTC](https://discuss.python.org/t/list-items-to-different-variables/16605/3 "2022-06-17T20:59:39Z")

</div>

later in code `i3` object may be used (in code someone else wrote). with this setup, then I can check if `i3` object exists etc. But good point, when bringing problem isolated, it don’t make much sense.

---

<div class="post-metadata">

**Author:** ![BowlOfRed](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/bowlofred/32/3379_2.png) [@BowlOfRed](https://discuss.python.org/u/BowlOfRed)\
**Post date:** [June 17, 2022, 9:19pm UTC](https://discuss.python.org/t/list-items-to-different-variables/16605/4 "2022-06-17T21:19:46Z")

</div>

I would say later in code the `i3` _name_ may be used. The object either exists or it doesn’t.

As such, at the interface to such code, you could assign to the name at that time. What would you expect to do in that section if the list were only 2 items long? If it’s going to throw an error anyway, you could just throw it earlier by doing:

```python
i1, i2, i3, i4 = my_list[:4]

```

Or is it somehow checking if i3 is assigned (which it wouldn’t be in the above examples)?

---

<div class="post-metadata">

**Author:** ![mlgtechuser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mlgtechuser/32/7310_2.png) [@mlgtechuser](https://discuss.python.org/u/mlgtechuser)\
**Post date:** [June 18, 2022, 7:20am UTC](https://discuss.python.org/t/list-items-to-different-variables/16605/5 "2022-06-18T07:20:01Z")

</div>

`exec()` takes a string and executes it as line of code. It is an ugly duckling but is a beautiful problem solver when there is no better way. Explained [HERE](https://www.programiz.com/python-programming/methods/built-in/exec).

```auto
for varidx, item in enumerate(my_list):
    exec('i'+str(varidx+1)+ '= item') #on first pass, executes 'i1 = item'

```

* * *

A small note here. An empty list will evaluate to a boolean `False`. Non-empty == `True`.

```auto
if list_len > 0: i1 = my_list[0] #these give
if my_list: i1 = my_list[0] #the same result

```

This boolean behavior is a bit hidden, though, and only readable by someone experienced enough to know about it.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [June 19, 2022, 5:41am UTC](https://discuss.python.org/t/list-items-to-different-variables/16605/6 "2022-06-19T05:41:25Z")

</div>

There are many ways to do this, and the choice between them is mostly a matter of personal taste. But if you absolutely know that the list _must_ have exactly 2 or 4 items, you can do this:

```
i1, i2 = mylist[:2]
i3, i4 = mylist[2:] or [None, None] # Use None as default for missing values

```

Alternatively:

```
i1, i2, *tmp = mylist
i3, i4 = tmp or [None, None]

```

One more:

```
i1, i2, i3, i4 = (mylist + [None, None])[:4]
```

---

<div class="post-metadata">

**Author:** ![mlgtechuser](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/mlgtechuser/32/7310_2.png) [@mlgtechuser](https://discuss.python.org/u/mlgtechuser)\
**Post date:** [June 19, 2022, 8:18am UTC](https://discuss.python.org/t/list-items-to-different-variables/16605/7 "2022-06-19T08:18:25Z")

</div>

> [@jannehof](#):
>
> is there a more pythonic way to do this?

> i1, i2, \*tmp = mylist  
> i3, i4 = tmp or [None, None]

This one suits my taste for being concise and readable with minimal mental gymnastics. The `exec()` loop has the benefit of parsing a data object with any number of members/elements but can be somewhat esoteric at first glance. The way Python offers you so many choices is wonderful.
