0

append vs. extend .I got answer here i just have to use extend keyword instead of append.

def extractDollar(line): 
        global mainList
        temp=[]

        #lowercasing all the string
        line=line.lower()

        #storing all word starting with $ in a line in temp
        #then adding that  to existing list mainList
        #to form a single list and removing empty value
        temp= re.findall(r'$\w+',line)

        mainList=mainList+[j for i in zip(mainList,temp) for j in i]
        mainList= filter(None, mainList)

        return line

I have a file with multiple string; each string has word starting with $ and I want to store all word starting with $ in a file as a single List (mainList).
I wrote this function to read file line by line. I am getting temp array filled with all value starting with $ in one line but not able to add all the single list returned by re.findall as single main List.

Community
  • 1
  • 1
Arjun Chaudhary
  • 2,373
  • 2
  • 19
  • 36

1 Answers1

0

Try reduce(sum, line):

def extractDollar(line): 
        global mainList
        temp=[]

        #lowercasing all the string
        line=line.lower()

        #storing all word starting with $ in a line in temp
        #then adding that  to existing list mainList
        #to form a single list and removing empty value
        temp= re.findall(r'$\w+',line)

        mainList=mainList+[j for i in zip(mainList,temp) for j in i]
        mainList= filter(None, mainList)

        return reduce(sum,line)
Vaibhav Sagar
  • 2,208
  • 15
  • 21