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

  • Does this answer your question? [Using "readlines()" twice in a row](https://stackoverflow.com/questions/10201008/using-readlines-twice-in-a-row) – Ahmed AEK Feb 07 '23 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()`. – Henry Woody Feb 07 '23 at 20:17

4 Answers4

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

  • 1
    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](https://docs.python.org/3/library/io.html#io.IOBase): "`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." – shadowtalker Feb 07 '23 at 21:21
  • I offer that the problem is more that OP fundamentally misunderstands loops in Python, moreso than their lack of reading the docs for `.readline()`. – shadowtalker Feb 07 '23 at 21:23
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()
maciek97x
  • 2,251
  • 2
  • 10
  • 21
0

Try this instead:

example_list = []

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

print(example_list)
Pedro Rocha
  • 1,373
  • 1
  • 3
  • 14
0

Shortest (and imho best) version:

with open("test.txt") as f:
    example_list = [line.strip() for line in f]
treuss
  • 1,913
  • 1
  • 15