python-3.xdata-scienceword-cloud

Difference between defining a variable as a=[a] and a= ' ' in python


I have below two doubts

1)I am trying to build a wordcloud and for doing that am defining a variable a=[ ] but it throws error but if I define it as a='' it works well. Please tell me what is the difference between them?

  1. I am using below two for loops but both of them show difference output whereas I expect them to show the same? What is the difference between them.

a)

allwords=[]
for i in data['Url']:
    allwords+= ' '.join(i)

b) all_words = ' '.join([text for text in data['Url']])


Solution

  • About item A

    There is an error in your code. To add an element to a list, use the append method:

    allwords=[]
    for i in data['Url']:
        allwords.append(' '.join(i))
    

    Use += to concatenate the string or append method to add an item to a list.

    About item B

    This works properly because is an attribution.