0

I just can't seem to solve this. I know others use this exact code and it works for them; why would I get an error and others not? I use Python 3.6.3

with open('Numbers.txt') as f:
    alist = [int(num) for num in f]

print(alist[0])
print(alist[99999])
print(alist())

Output:

54044
91901
Traceback (most recent call last):
  File "test3.py", line 8, in <module>
    print(alist())
TypeError: 'list' object is not callable

Also did below with the same error:

with open('Numbers.txt') as f:
    numbers = f.readlines()

numbers = [int(x.strip()) for x in numbers]

print(numbers[0])
print(len(numbers()))
4
  • 1
    "I know others use this exact code and it works for them". Are you 100% sure about that? Do you have a link? Commented Nov 3, 2017 at 17:33
  • It is clear, lists are not callable, so you can not use () Commented Nov 3, 2017 at 17:36
  • coursera.org/learn/algorithms-divide-conquer/discussions/all/… Commented Nov 3, 2017 at 17:40
  • I'm expecting to see a list of 100000 integers. I want to use it in a Sort algorithm. The algorithm works with test array inputs, but not with this list. Commented Nov 3, 2017 at 17:42

3 Answers 3

3

You're calling the list on line 8, see alist(). Lists aren't callable. If you want the full list, just do alist.

Sign up to request clarification or add additional context in comments.

Comments

0

Your syntax is invalid. You used parentheses instead of brackets.

alist()

Is not a valid use; the parentheses signal to the parser that you want to use alist as a function. What do you want this to do?

The second one has the same problem. For that, I think you just want the name without parentheses:

print (len(numbers))

1 Comment

Thanks, I'm embarrassed, should have picked that up.
0

In Python, () indicates that you want to call or execute something, a function for example. You were trying to call numbers by numbers() in the last line, which is a list, not a function. Change it to numbers to get the length of the list numbers.

with open('Numbers.txt') as f:
    numbers = f.readlines()

numbers = [int(x.strip()) for x in numbers]

print(numbers[0])
print(len(numbers))

1 Comment

Please explain what/how your contribution solves the OPs question.

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.