May I drop list bracket from list?

Dave Angel davea at davea.name
Thu Apr 23 10:05:23 EDT 2015


On 04/23/2015 06:11 AM, subhabrata.banerji at gmail.com wrote:
> Dear Group,
>
> I am trying to read a list of files as
> list_of_files = glob.glob('C:\Python27\*.*')
> Now I am trying to read each one of them,
> convert into list of words, and append to a list
> as.
>
> list1=[]
> for file in list_of_files:
>        print file
>        fread1=open(file,"r").read()
>        fword=fread1.split()
>        list1.append(fword)
>
> Here the list is a list of lists, but I want only one list not
> list of lists.
>
> I was thinking of stripping it as, str(list1).strip('[]')
>
> but in that case it would be converted to string.
>
> Is there a way to do it. I am using Python27 on Windows7 Professional.
> Apology for an indentation error.
>
> If anybody may please suggest.
>

You're first problem is the name of your variable.  fword implies it's a 
string, but it's really a list.  So when you do:
      list1.append(fword)

you're appending a list to a list, which gives you nested lists.  Sounds 
like you want
      list1.extend(fword)



-- 
DaveA



More information about the Python-list mailing list