CS 111
Summer 2 2018

Problem Set 1 FAQ

Problem Set 1 FAQ

If you don’t see your question here, post it on Piazza or come to office hours! See the links in the navigation bar for both of those options.

General questions

  1. I get an error message that mentions a TypeError when I add something to a list. What should I do?

    If you get something like the following error.

    TypeError: can only concatenate list (not "int") to list
    

    you are likely trying to add an integer to a list.

    Suppose you have two lists called xs and ys.

    x = [0, 1, 2]
    y = [3, 4, 5]
    

    And you would like to make a list that contains [0, 1, 2, 3]. You might try doing the following:

    x + y[0]
    

    Unfortunately, this is not allowed in Python, because addition between a list and an integer is not defined. If you think about it, this makes sense: how would you add a list to an integer? Instead, recall that the + operator can be used to concatenate two lists. Knowing this, we can put y[0] inside its own list and concatenate that with the list x.

    x + [y[0]]