Python List : Index out of Range
<p>I've been trying to create a program that has to read in a file, find the unique words and punctuation, put those to a list and then get the positions of each word and store them in a list. Then, using the lists the program will recreate the file. This is my code:</p>

<pre><code>import time
import re
words = open('words.txt')
sentence = words.read()
uniquewords = []
positions = []
punctuation = re.findall(r"[\w']+|[.,!?;]", sentence)
for word in punctuation:
    if word not in uniquewords:
        uniquewords.append(word)
print("This file contains the words and punctuation ", uniquewords)
positions = [uniquewords.index(word) for word in punctuation]
recreated = " ".join([uniquewords[i] for i in positions])
print("In a list the text file words.txt can be shown as:")
print(positions)
print("Recreating sentence...")
print(recreated)
</code></pre>

<p>The program above does what it needs to, except it produces the following output:</p>

<blockquote>
  <p>This file contains the words and punctuation  ['Ask', 'not', 'what',
  'your', 'country', 'can', 'do', 'for', 'you', ',', '!'] </p>
  
  <p>In a list the
  text file words.txt can be shown as:</p>
  
  <p>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 2, 8, 5, 6, 7, 3, 4, 10]</p>
  
  <p>Recreating sentence...</p>
  
  <p>Ask not what your country can do for you , Ask what you can do for
  your country !</p>
</blockquote>

<p>The positions list starts at 0, so as normal I tried just doing this:</p>

<pre><code>positions = [uniquewords.index(word)+1 for word in punctuation]
</code></pre>

<p>However this produces the error</p>

<pre><code>  File "C:\Users\Sam\Desktop\COMPUTING TEMP FOLDER\task 3.py", line 13, in &lt;module&gt;
    recreated = " ".join([uniquewords[i] for i in positions])
  File "C:\Users\Sam\Desktop\COMPUTING TEMP FOLDER\task 3.py", line 13, in &lt;listcomp&gt;
    recreated = " ".join([uniquewords[i] for i in positions])
IndexError: list index out of range
</code></pre>

<p><strong>How can I make the list start at 1 without getting this error? Any help would be greatly appreciated.</strong> </p>

<p>Another small problem is that while the original string is </p>

<p>"Ask not what your country can do for you, Ask what you can do for
your country!"</p>

<p>the actual output is instead </p>

<p>Ask not what your country can do for you , Ask what you can do for your country !</p>

<p>Please check the below code. I changed bit for recreating string to solve space issue along with the indexing problem you were facing.</p>

<pre><code>import time
import re
words = open("val.txt",'r')
sentence = words.readline()
uniquewords = []
positions = []
punctuation = re.findall(r"[\w']+|[.,!?;]", sentence)
for word in punctuation:
    if word not in uniquewords:
        uniquewords.append(word)
print("This file contains the words and punctuation ", uniquewords)
positions = [uniquewords.index(word)+1 for word in punctuation]
#recreated = " ".join([uniquewords[i-1] for i in positions])
recreated = ''
for i in positions:
     w = uniquewords[i-1]
     if w not in '.,!?;':
          w = ' ' + w
     recreated = (recreated + w).strip()

print("In a list the text file words.txt can be shown as:")
print(positions)
print("Recreating sentence...")
print(recreated)
</code></pre>

<p>Output:</p>

<pre><code>    C:\Users\dinesh_pundkar\Desktop&gt;python c.py
('This file contains the words and punctuation ', ['Ask', 'not', 'what', 'your',
 'country', 'can', 'do', 'for', 'you', ',', '!'])
In a list the text file words.txt can be shown as:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 3, 9, 6, 7, 8, 4, 5, 11]
Recreating sentence...
Ask not what your country can do for you, Ask what you can do for your country!
</code></pre>

python