'Problem with .append() function for strings

I am having some problems with a basic code I am running: I want to append in a string array (ppn_list) some terms deriving from a DataFrame (affs_til_temp.parent_preferred_name). Here is the code:



ppn_list = []

for i in range(15):
  word_ppn = affs_til_temp.parent_preferred_name[i]
  if word_ppn != None:
    for j in ppn_list:
      if not re.search(j,word_ppn):
        ppn_list.append(word_ppn)

The problem is that for identical "word_ppn" (e.g. 'Princeton University') the code continues appending this term on the list.

print(ppn_list)

[ 'Princeton University', 'Princeton University', 'Princeton University', 'Princeton University', 'Princeton University', 'Princeton University', 'Princeton University', 'Princeton University', 'Princeton University', 'Lawrence Livermore National Laboratory', 'Lawrence Livermore National Laboratory', 'Lawrence Livermore National Laboratory', 'Lawrence Livermore National Laboratory', 'Lawrence Livermore National Laboratory', 'Lawrence Livermore National Laboratory', 'Lawrence Livermore National Laboratory', 'Lawrence Livermore National Laboratory', 'Lawrence Livermore National Laboratory', 'Lawrence Livermore National Laboratory', 'Princeton University', 'Princeton University', 'Princeton University', 'Princeton University', 'Princeton University', 'Princeton University', 'Princeton University', 'Princeton University', 'Princeton University', 'Princeton University', 'Princeton University']



Solution 1:[1]

you have issue with logic in the for j loop - you add one word_ppn for each element in ppn_list which does not match word_ppn. Instead you should check if any word in ppn_list matches ppn_word and if not - add one to the list. Something like:

ppn_list = []

for i in range(15):
     word_ppn = affs_til_temp.parent_preferred_name[i]
     if word_ppn != None:
         if not any( re.search(word_ppn,j) for j in ppn_list):
             ppn_list.append(word_ppn)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Maciej Wrobel