Python: Regex findall returns a list, why is trying to access the list item [0], return an error?

The documentation below shows a snippet showing how the regex search method works and confirms that it returns a list.

re.findall(r"\w+ly", text) ['carefully', 'quickly'] 

However, the following code fragment generates an error outside the bounds ( IndexError: list index out of range ) when trying to access the null element of the list returned by the findall function.

Corresponding code snippet:

 population = re.findall(",([0-9]*),",line) x = population[0] thelist.append([city,x]) 

Why is this happening?

For some background, how this snippet fits into my entire script:

 import re thelist = list() with open('Raw.txt','r') as f: for line in f: if line[1].isdigit(): city = re.findall("\"(.*?)\s*\(",line) population = re.findall(",([0-9]*),",line) x = population[0] thelist.append([city,x]) with open('Sorted.txt','w') as g: for item in thelist: string = item[0], ', '.join(map(str, item[1:])) print string 

EDIT: Read the comment below to find out why this happened. My quick solution:

 if population: x = population[0] thelist.append([city,x]) 
+7
source share
3 answers

re.findall will return an empty list if there are no matches:

 >>> re.findall(r'\w+ly', 'this does not work') [] 
+11
source

re.findall may return you an empty list if there was no match. If you try to access [][0] , you will see that IndexError .

In order to ignore matches, you should use something line by line:

 match = re.findall(...) if match: # potato potato 
+3
source

I had the same problem. The solution seems very simple, and I do not know why I did not consider it.

 if match: 

instead

 if match[0]: 
+1
source

All Articles