Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9008657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:59:37+00:00 2026-06-16T01:59:37+00:00

I am just testing out how lists work in python and I find it

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:59:38+00:00Added an answer on June 16, 2026 at 1:59 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm just testing out the csv component in python, and i am having some
I am just getting started with JS Unit Testing and am trying out qUnit.
I have just started out with testing some php mvc framework In it, it
I'm new in QT, and I'm just testing out the MOC. For a given
Ok so I am testing out EF once again for performance and I just
I have a script that I wrote in python for testing out the sorting
I am testing out the very awesome Knockoutjs. I am facing a peculiar problem.
I was just testing Towers of Hanoi problem in Java and I ran the
I am just testing and trying to learn how assembler works with C. So
I'm just testing a simple databind expression with: <div> Now: <%# DateTime.Now.ToString()%> </div> According

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.