Why does this code fail? assigning variable to list of itself in for loop

a = 'hello'
b = None
c = None
for x in [a, b, c]:
    if isinstance(x, (basestring, dict, int, float)) or x is None:
        x = [x]
a

returns 'hello', but expected ['hello']. However, this works:

a = 'hello'
a = [a]
a

returns ['hello'].


Solution 1:

To achieve that, first you have to understand that you have two diferent refecerences, a and x (for each element), and the reference for the list [a,b,c], used only in the for loop, and never more.

To achieve your goal, you could do this:

a = 'hello'
b = None
c = None
lst = [a, b, c] #here I create a reference for a list containing the three references above
for i,x in enumerate(lst):
    if isinstance(x, (str,dict, int, float)) or x is None: #here I used str instead basestring
        lst[i] = [x]

print(lst[0]) #here I print the reference inside the list, that's why the value is now showed modified

['hello']

but as I said, if you do print(a) it will show again:

'hello' #here i printed the value of the variable a, wich has no modification

because you never did anything with it.

Take a look at this question to understand a little more about references How do I pass a variable by reference?

Solution 2:

Lets work through this one line at a time;

a = 'hello'  # assigns 'hello' to a.
b = None  # etc...
c = None
for x in [a, b, c]:  # Creates list from the values in 'a' 'b' and 'c', then iterates over them.
    # This if statement is always True. x is always either a string, or None.
    if isinstance(x, (basestring, dict, int, float)) or x is None:
        x = [x]  # On the first iteration, sets variable to the expected ['hello']
        # After that though - it replaces it with [None]
a  # = 'hello' - you have not assigned anything to the variable except on the first line.

The only variable that is ever set to ['hello'] is x, which is quickly overwritten with None. If you changed your if check to exclude or x is None and assigned to a instead of x you would get your desired result.

It's also worth noting that the list [a, b, c] is created when you start the for loop. Changing a b or c during the for loop will have no effect - the list has already been created.