user1174868
user1174868

Reputation:

Appending to a list to remove elements from the list

I am just testing out how lists work in python and I find it to be very confusing and frustrating. For whatever reason I am getting constant error messages on my code.

def listtest(list1,x):
    list2 = []
    count = 0
    for n in list1:
        if list1[count] == x:
            count += 1
        else:
            list2 = list.append(list2,list[count])
            count += 1
    return list2

For whatever reason it is either telling me that list.append only accepts one argument which is confusing. In the interactions window I can call append with two arguments like so list.append(list,3) or whatever and it works flawlessly. The other error I am getting is

list2 = list.append(list2,list[count])
TypeError: 'type' object has no attribute '__getitem__'

which is entirely nonsensical to me and not in any way helpful. What is wrong with my code? Why does the interactions window behave differently than the other window? Why won't append work like it does in the interactions window?

Upvotes: 1

Views: 637

Answers (4)

Tim Pietzcker
Tim Pietzcker

Reputation: 336198

Edit: Rewritten after some more careful analysis:

Your immediate problem is list[count] which should be list1[count]. The built-in type list can't be indexed (since it's a type, not a list), so you get the TypeError about the __getitem__() method not being supported.

But even if you had written list2 = list.append(list2,list1[count]), you'd still have a problem. What would that line of code do?

  1. Append list1[count] to list2. So far, so good.
  2. Assign the result of the call to list.append() to list2. Since .append() is a method that modifies the object it's called on in-place, it always returns None.
  3. So now list2 is None.
  4. The next time you call that line, you'll get another TypeError because you can't append anything to NoneType objects.

So, what you should have written is

list2.append(list1[count])

While that would now work, it's an extremely roundabout way to do this. Keeping track of indexes you don't actually need is very unpythonic - the language is much more expressive than that. Don't try to write Java programs in Python. Your function (if your aim really is to create a new list that contains all the objects in list1 that are the same as x - for which I fail to see the point entirely) could be written as

def listtest(list1, x):
    return [item for item in list1 if item == x]

although that's not much more useful than simply writing

list1.count(x)

which gives you the number of times x appears in list1.

Upvotes: 2

Roland Smith
Roland Smith

Reputation: 43505

If you want to append to list2, use list2.append(), not list.append().

Upvotes: 1

g.d.d.c
g.d.d.c

Reputation: 47988

Your errors aren't nonsensical, they're trying to point you to the underlying issue. You should not use list as a variable name, because list is a built-in type. Additionally, when you attempt to append to a list you do only want to pass one argument. You can append a tuple like so:

myList.append(('val1', 'val2'))

Upvotes: 1

user2665694
user2665694

Reputation:

You can only append one item at a time to list. You can extend() a list with the items of a second list which basically means that you append each individual item of the second list to the first one. What is here hard to understand? The list API is very well documented and clear.

Upvotes: 0

Related Questions