Hello

Sorry about the stupidity of my question, I'm rather new to python.

I'm writing a text analysation program. It reads from a text file and creates a list called 'line' which has variable length and is a list of words. I want to make a loop that will go through from index 2 to the end and see if the words match any from another list. I can't seem to work out how to loop through from 2 to the end though. I've tried a few different ways of looping through to the end using for loops and while loops, but I end up with IndexError, or TypeError. And I have no idea how to start at 2.

I don't really know what to try now, so I'd love a few hints.

Thanks

Recommended Answers

All 2 Replies

Member Avatar for Enalicho

Use slicing -

for line in lines[2:]:
    for word in line:
        print word

This'll iterate through the items in lines starting at index 2 ending at index len(lines) - 1.

Note, indexing in Python starts at 0 - so to start at index 2 will actually mean it starts at the third element in the sequence.

If you want more help, post your code.

That's great. Slicing was exactly what I needed. Thanks!
Sorry for asking such a simple question, I really should have been able to search that up.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.