How to combine multiple try and except in Python

Viewed 158

I have a list that will sometimes hold a single value, sometimes 2 or 3 values. I am assigning each of the values to a variable. If there is no value for the assigned position of the list, I want it to be a empty variable. Is there any way I can make this more efficient?

split = line.split(',')
try:
    var1 = split[0]
except IndexError:
    var1 = None
    
try:
    var2 = split[1]
except IndexError:
    var2 = None

try:
    var3 = split[2]
except IndexError:
    var3 = None
3 Answers
split = line.split(',')
variables = [None, None, None]


for i in range(len(split)):
        variables[i] = split[i]

var1, var2, var3 = variables

Edit (after @ekhumoro):

split = line.split(',')
variables = [None, None, None]
variables[:len(split)] = split
var1, var2, var3 = variables

You can use generator expression (or list comprehension) with conditional check to see if the index exists in the list. If exists, return element at that index, else return None. Here I am limiting my generator expression to return n (which is 3 in your case) values, and you can unpack and save these values in n variables.

For example:

my_list = [1] # List with just one element
n = 3  # Number of variables in which you want to fill the values

var1, var2, var3 = (my_list[i] if i < len(my_list) else None for i in range(n))

print(var1, var2, var3)
# prints: (1, None, None)

Refer Generator Expressions and List Comprehensions for more details.

Depending on what you're actually trying to accomplish*, it might be better to not use individual variables. For example, you could use dict.get(), which returns None if the given key is not in the dict.

for line in 'a,b,c', 'd,e', 'f':
    split = line.split(',')
    d = {i: v for i, v in enumerate(split)}
    print(d, ' -> ', d.get(0), d.get(1), d.get(2))

Output:

{0: 'a', 1: 'b', 2: 'c'}  ->  a b c
{0: 'd', 1: 'e'}  ->  d e None
{0: 'f'}  ->  f None None

You could also consider using unpacking:

for line in 'a,b,c', 'd,e', 'f':
    var1, *others = line.split(',')
    print(var1, others)

Output:

a ['b', 'c']
d ['e']
f []

By the way, note that split[0] will always exist, so I'm not sure why you're expecting an IndexError. For example:

>>> ''.split(',')
['']

* See some related ideas at How do I create variable variables? and more broadly, how to avoid the XY problem

Related