Skip to main content

Section 9.7 Concatenation and Repetition

Subsection 9.7.1 Concatenation

As with strings, the + operator concatenates lists and tuples.
It is important to see that these operators create new lists/tuples from the elements of the operand lists/tuples. If you concatenate a list with 2 items and a list with 4 items, you will get a new list with 6 items (not a list with two sublists).
One way for us to make this more clear is to run the following example in CodeLens. As you step through the code, you will see the variables being created and the lists that they refer to. Pay particular attention to the fact that when new_list is created by the statement new_list = fruit_list + num_list, it refers to a completely new list formed by making copies of the items from fruit_list and num_list. You can see this very clearly in the CodeLens object diagram. The objects are different.

Note 9.7.1.

WP: Adding types together
Beware when adding different types together! Python doesn’t understand how to concatenate different types together. Thus, if we try to add a string to a list with ['first'] + "second" then the interpreter will return an error. To do this you’ll need to make the two objects the same type. In this case, it means putting the string into its own list and then adding the two together like so: ['first'] + ["second"]. This process will look different for other types though. Remember that there are functions to convert types!
The following code won’t run, because the third line attempts to add two different types (a list and a tuple). Run it to see the error.
Fix the error above by converting veggies1 to a list in the expression on line 3: list(veggies1)

Subsection 9.7.2 Repetition

The * operator repeats the items in a list or tuple a given number of times. So, repetition of a list of 2 items 4 times will give a list with 8 items.
As with concatenation, these operations do not have any effect on the original lists/tuples, they create new lists. You can reassign the new list back to the same variable name though:
Check your understanding

Checkpoint 9.7.2.

    What is printed by the following statements?
    alist = [1,3,5]
    blist = [2,4,6]
    print(alist + blist)
    
  • 6
  • Concatenation does not add the lengths of the lists.
  • [1,2,3,4,5,6]
  • Concatenation does not reorder the items.
  • [1,3,5,2,4,6]
  • Yes, a new list with all the items of the first list followed by all those from the second.
  • [3,7,11]
  • Concatenation does not add the individual items.

Checkpoint 9.7.3.

    What is printed by the following statements?
    alist = [1,3,5]
    print(alist * 3)
    
  • 9
  • Repetition does not multiply the lengths of the lists. It repeats the items.
  • [1,1,1,3,3,3,5,5,5]
  • Repetition does not repeat each item individually.
  • [1,3,5,1,3,5,1,3,5]
  • Yes, the items of the list are repeated 3 times, one after another.
  • [3,9,15]
  • Repetition does not multiply the individual items.
You have attempted of activities on this page.