2

I must be missing something because this is strange...

a = ['a', 'b', 'c']
a1 = ['b', 'a']
foo = list( set(a) - set(a1))

** returning **

foo == ['c']
type(foo) == <type 'list'>
foo[0] == 'c'

** now the strange part **

foo = foo.insert(0, 'z')
foo == None

why do list operations like insert, and append cause foo to be None??

the following accomplishes what my top example attempts to but seems ridiculous.

import itertools

a = ['a', 'b', 'c']
a1 = ['b', 'a']

foo = list(set(a) - set(a1))
q = [['z']]
q.append(foo)
q = [i for i in itertools.chain.from_iterable(q)]
q == ['z', 'c']

any insight would be appreciated. thank you.

4
  • 2
    I can't reproduce this, foo==None returns False. Commented Feb 11, 2014 at 19:54
  • did you type foo = foo.insert(0, 'z') by accident? that would cause it to be None. Commented Feb 11, 2014 at 20:16
  • Corley Brigman is correct. I was essentially assigning my list variable the return value of .insert(), which as NPE pointed out is in fact None.. Commented Feb 11, 2014 at 20:58
  • I edited my question to reflect Corley Brigman's comment. Changing foo.insert(0, 'z') to foo = foo.insert(0, 'z'). Thanks everyone Commented Feb 11, 2014 at 21:00

1 Answer 1

10

foo.insert() returns None, but does change foo in the way you'd expect:

>>> foo = ['c']
>>> foo.insert(0, 'z')
>>> foo
['z', 'c']

If you wish to assign the result to a different variable, here is one way to do it:

>>> foo = ['c']
>>> bar = ['z'] + foo
>>> bar
['z', 'c']
Sign up to request clarification or add additional context in comments.

2 Comments

+1, are you perhaps doing print foo.insert(0, 'z')?
the foo.inster(0, 'z') was being used as the return statement in a function. So it was literally ... return foo.insert(0, 'z'). Which explains why I was getting None.. And proves how painfully oblivious I can be sometimes.. Thank you for pointing this out to me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.