1

I am trying to get each value from data.txt into my list. Unfortunately, I do not understand why For Loop does not work in the following case:

#(data length is 8)

example_list = []

with open("/path/data.txt") as example:

    for number in range(example.readlines())):
        example_list.append(example.readlines()[number])

print(example_list)

Error list index out of range appears.

When in stead of For Loop I manually insert "number" (from 0 to 8) it does work, therefore I do not understand why it does not work with for loop

Appreciate any help on that

2
  • Does this answer your question? Using "readlines()" twice in a row Commented Feb 7, 2023 at 20:17
  • example.readlines() already gives you a list of the lines, you don't need to loop over and create a new list. You can just do example_list = example.readlines(). Commented Feb 7, 2023 at 20:17

4 Answers 4

1

If you are going to iterate over the contents of a file, you first have to check what file structure the method you use returns. In this case, readlines() returns a list. Link.

You can not use a list as an argument for range() because it expects an integer.

So you need to iterate over that list returned by readlines(), not over the file itself.

example_list = []

with open("file.txt") as example:
    # lines is a list
    lines = example.readlines()
    for item in lines:
        # add each element to the example_list
        example_list.append(item)

print("example list: ", example_list)

You can also use example_list.append(item.strip()) to get cleaner data. Link

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

2 Comments

Note that over a file-like object does already iterate over lines. The .readlines() method simply collects all lines into a list. See e.g. here: "IOBase (and its subclasses) supports the iterator protocol, meaning that an IOBase object can be iterated over yielding the lines in a stream. Lines are defined slightly differently depending on whether the stream is a binary stream (yielding bytes), or a text stream (yielding character strings). See readline() below."
I offer that the problem is more that OP fundamentally misunderstands loops in Python, moreso than their lack of reading the docs for .readline().
0

You can iterate over file's lines like this:

example_list = []

with open("/path/data.txt") as example:
    for number in example:
        example_list.append(number)

print(example_list)

Or simplier

example_list = open("/path/data.txt").readlines()

Comments

0

Try this instead:

example_list = []

with open("test.txt") as f:
    for line in f:
        example_list.append(line.strip())

print(example_list)

Comments

0

Shortest (and imho best) version:

with open("test.txt") as f:
    example_list = [line.strip() for line in f]

Comments

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.