Question: **code:**

```
with open(filename) as f:
    file_list = f.readlines()
    file_list = [line.strip() for line in file_list] # remove whitespaces from each line of file
    code to process data between start and end tags (these tags can have whitespaces thats why i have removed them above)

```

*This code works fine for me* but if the file is too big then i don't think its sensible to copy whole data in a list then strip whitespaces from each line.

*How can i remove whitespaces for specific part of list so that only that much part i can save in list ?*

I tried:

```
with open(filename) as f:
    for line in f.readlines():
         if line.strip() == "start":
             start = f.readlines.index("start")
         if line.strip() == "end"
             end = f.readlines.index("end")
    file_list = f.readlines[start:end]

```

But its giving error

```
start = f.readlines.index("start")
AttributeError: 'builtin_function_or_method' object has no attribute 'index'

```

I just want to write an efficient code of code mentioned on top of this post.

Answer: 
The problem with your code is that the file object `f` is an iterator, and once you call `f.readlines()` it is exhausted, so finding the index of a line by calling `f.readlines()` *again* can't work. Also, calling `readlines()` *at all* negates your effort of storing only the interesting parts of the file, as `readlines()` would read the entire file into memory anyways.

Instead, just memorize whether you've already seen the start-line and add the following lines to the list until you see the end-line.

```
with open(filename) as f:
    started, lines = False, []
    for line in f:
        stripped = line.strip()
        if stripped == "end": break
        if started: lines.append(stripped)
        if stripped == "start": started = True

```

Alternatively, you could also use [`itertools.takewhile`](https://docs.python.org/3/library/itertools.html#itertools.takewhile) to get all the lines up to the end-line.

```
import itertools
with open(filename) as f:
    for line in f:
        if line.strip() == "start":
            lines = itertools.takewhile(lambda l: l.strip() != "end", f)
            lines = map(str.strip, lines)
            break

```

Or even shorter, using another `takewhile` to read (and discard) the lines before the start-line:

```
with open("test.txt") as f:
    list(itertools.takewhile(lambda l: l.strip() != "start", f))
    lines = itertools.takewhile(lambda l: l.strip() != "end", f)
    lines = map(str.strip, lines)

```

In all cases, `lines` holds the (stripped) lines between the start- and the end-line, both exclusive.
Tobias's first answer can be modified a bit with `continue` ...

```
with open(filename) as f:
    started, lines = False, []
    for line in f:
        stripped = line.strip()
        if stripped == "end": break
        if stripped == "start": 
            started = True
            continue
        if not started: continue

        # process line here no need to store it in a list ...

```