0

I'm trying to understand why, computationally, using += to add to a list, where square brackets have not been used to encapsulate the value, results in a single character at a time being added to the list as an element.

I hope that the question is clear; here is an example:

In:


def generate_sentences(subjects, predicates, objects):
    lst1 = []
    lst2 = []
    lst3 = []
    lst4 = []
    lst5 = []
    subjects = sorted(subjects)
    predicates = sorted(predicates)
    objects = sorted(objects)
    for i in subjects:
        for j in predicates:
            for k in objects:
                lst1 += i + " "
                lst2 += (i + " ")
                lst3 += [i + " "]
                lst4.append(i + " ")
                lst5.append([i + " "])


    print("+= no paren: ")
    print(lst1)
    print(" ")
    print("+= paren: ")
    print(lst2)
    print(" ")
    print("+= brackets: ")
    print(lst3)
    print(" ")
    print("append standard: ")
    print(lst4)
    print(" ")
    print("append with brackets: ")
    print(lst5)



generate_sentences(["John", "Mary"], ["hates", "loves"],\
    ["apples", "bananas"])

generate_sentences(["Vlad", "Hubie"], ["drives"],\
["car", "motorcycle", "bus"])

and

Out:


+= no paren: 
['H', 'u', 'b', 'i', 'e', ' ', 'H', 'u', 'b', 'i', 'e', ' ', 'H', 'u', 'b', 'i', 'e', ' ', 'V', 'l', 'a', 'd', ' ', 'V', 'l', 'a', 'd', ' ', 'V', 'l', 'a', 'd', ' ']

+= paren: 
['H', 'u', 'b', 'i', 'e', ' ', 'H', 'u', 'b', 'i', 'e', ' ', 'H', 'u', 'b', 'i', 'e', ' ', 'V', 'l', 'a', 'd', ' ', 'V', 'l', 'a', 'd', ' ', 'V', 'l', 'a', 'd', ' ']

+= brackets: 
['Hubie ', 'Hubie ', 'Hubie ', 'Vlad ', 'Vlad ', 'Vlad ']

append standard: 
['Hubie ', 'Hubie ', 'Hubie ', 'Vlad ', 'Vlad ', 'Vlad ']

append with brackets: 
[['Hubie '], ['Hubie '], ['Hubie '], ['Vlad '], ['Vlad '], ['Vlad ']]
1
  • 3
    Isn't it because the list object on the left hand side makes the addition operator iterate over the result on the right? I'm surprised it even worked but, that would probably be why. Commented Jun 2, 2020 at 11:54

1 Answer 1

1

I think I understand your issue, let me know if I got this correct or not :). So there are several things to this behavior, First, the difference between append and += is that :

+= merges 2 lists into one list.

append adds an element to a list.

So fundamentally they are not the same thing.

Second, a string is basically a list of characters, so when you are doing this :

mylist += "a word"

You are basically doing this :

mylist += ['a', ' ', 'w', 'o', 'r', 'd']

Try adding an int to your list with a +=, this will occur :

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not iterable

On the other end if you use append to add an integer, it will work because it is not adding a list but an item that can be any type.

I think I answered your question, let me know if you need clarification :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.