Transcript for:
Understanding String and List Concatenation in Python

You have already used the concatenation operator, or the plus, to create a new string by concatenating two other strings together. For example, a string equals the string abc plus the string def will create a new string containing all of the characters from abc and def and assign that string to the variable a string. Similarly, the concatenation operator can be used to create a new list by concatenating two other lists together. For example, blist equals the list containing a and b plus the list containing c and d will create a new list that contains all four elements a, b, c, and d and assign it to the variable blist. The concatenation operator only works when you are combining two lists together. In other words, you can only concatenate one list onto another. If you would like to concatenate some other kind of value onto your list, you should enclose the value in a literal list. For example, alist equals alist plus 5 will cause a type error because you cannot use the plus operator with a list and an integer together. But alist equals alist plus the literal list containing the element 5 will work. This will effectively append the value 5 onto the contents of alist. The original list is not changed by the concatenation operation. The concatenation operator or plus sign can be used to create a new list that contains all of the elements in two other lists. The original lists are not changed in any way. You may reassign one of your variables to the newly created list, but that is not the same as changing the original list. The variable changes to the new list, but the original list is not modified. Concatenating a value that is not a list will cause a type error. The value should be enclosed in a literal list so that it can be concatenated onto the list, as in this example where we enclose the value rest inside of a list before concatenating it onto the first list. As usual, the Python interpreter is a really great place to play around with a new feature of Python, including the list concatenation operator. So to start, let's create a couple of basic lists. And now let's use these lists to experiment with concatenation. So first I'm going to create a third list, list C, by combining the elements in A and B together using the concatenation operator. If we take a look at the contents of A and B, they have been unmodified. However, the list C now contains all of the elements from both lists concatenated together in order. So all of the elements from list A first, followed by all the elements from list B. Now, if I wanted to add, let's say, the letter D to the end of my list C, I can't do that using concatenation because I'm not allowed to concatenate something that is not a list with a list. So instead of concatenating the value directly, I can enclose it in a literal list. So this literal list just contains the value d and I'm concatenating that with the current value of c. When we take a look at c, it now has the value d appended onto it. At this point, you might be saying, now, wait a minute. I thought you said you cannot modify an existing list using concatenation. But I appear to have done that. I've taken the contents of the list c and I've appended a d onto the end of it. But that's not really what I did. When I executed the c plus. list containing d, I created a brand new list containing all of the elements from the original list c plus that d concatenated onto the end. I then changed my variable c to point to the new list. This did not modify the list that c referred to, instead it changed c to refer to a new list, and we can prove that pretty easily. Let me start over by setting c equal to a plus b. We can now see that it contains all of the elements from A and all of the elements from B, and I'll remind you that A and B have not changed in any way. Now I'm going to create a fourth list, D, but D is going to be a reference to the same list as C. These are two references to exactly the same list. For example, if I change the value at index 0 in C so that it is a 5, When I take a look at d, the value at index 0 has also changed in d. That's because these are two variables that refer to the same list. Now I'm going to concatenate a new value onto list c. So I can see that the list c now has that value 7 appended onto the end of it. But if I take a look at d, and remember d pointed at the original... list c, it has not been modified. So again what that concatenation operator did was create a new list with all of the elements from c and then it concatenated a list with the value 7 onto the end of it. So that new list was then used to change the variable c to refer to it. So c no longer refers to the original list but d still does and so d shows that the original list has not been modified.