Taking the 'product' of lists

Question:

Suppose that I am given a list of strings, e.g. list = ['a', 'b', 'c']. I am also given a list of ‘continuation strings’, e.g. continuations = ['d', 'f'], and I want to form a list of all possible sequences formed by combining the original list with a continuation letter. In this example, I want to obtain the list of lists: new_list = [['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'f']]. To do this, I tried

new_list = []
for element in continuations:
    # Make a copy of the original list
    copy = list
    # Add a continuation letter to the original list
    possible_sequence = copy.append(element)
    # Add the new list to the list of lists
    new_list.append(possible_sequence)

But this generates [None, None]… Can anyone explain what is wrong with my code?

Asked By: afreelunch

||

Answers:

Here’s how I would do it.

  1. create a list to store the possible sequences
  2. iterate through the continuation list
  3. copy the original list
  4. append the continuation letter to the copied list
  5. append the copied list to the possible list
def combine_list(list_, cont_list):
    # create a list to store the possible sequences
    possible_list = []
    # iterate through the continuation list
    for j in cont_list:
        # copy the original list
        l2 = list_.copy()
        # append the continuation letter to the copied list
        l2.append(j)
        # append the copied list to the possible list
        possible_list.append(l2)
    return possible_list

l = ['a', 'b', 'c']
c = ['d', 'f']
print(combine_list(l, c))

Output:

[['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'f']]

Edit


What’s wrong with your code?

  1. If you want to copy a list you need to it with list.copy(). If you just do copy = list you are not creating a new list object. if you make changes in copy all changes will apply to list also.

  2. The list.append(element) function does not return a list object it returns None that’s why your result looks like this [None, None] you appended None twice.

Answered By: noah1400

CODE

main_list = ['a', 'b', 'c']
continuations = ['d', 'f']

new_list = []
for element in continuations:
    temp_list = main_list.copy()
    temp_list.append(element)
    new_list.append(temp_list)

print(new_list)

OUTPUT

[['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'f']]
Answered By: vignesh kanakavalli
# it is a bad practice to shadows built-in name, so I changed 'list' name to 'abc_list'
abc_list = ['a', 'b', 'c']
continuation = ['d', 'f']
print([abc_list + [x] for x in continuation])

Output: [['a', 'b', 'c', 'd'], ['a', 'b', 'c', 'f']]

Answered By: ZabielskiGabriel

In Python, append modifies the list it is called on and doesn’t return anything (technically it returns None, which is why you ended up with a list full of None). That means that you cannot store the result of append in a variable.

my_list = []
foo = my_list.append(1)
print(foo)        # Prints 'None' because that's all that append returns
print(my_list)    # Prints '[1]' because the value got added to the original list by append

This is the big difference between lists and strings in Python that beginners sometimes get confused about. Strings are immutable which means they cannot be changed. So methods such as replace return a new string, because they cannot modify the original string. Lists, on the other hand, are mutable, meaning they can be modified. So methods on lists such as append or pop modify the list they are called on rather than returning a new one.

my_string = "Python"

# Needs to be stored in a new variable,
# the original string cannot be modified
new_string = my_string.replace("n", "k")

print(my_string)    # Still the original value, Python
print(new_string)   # The new modified value, Pythok

my_list = [1, 2]
my_list.append(3)   # Modified the list itself, no need to store anything new
print(my_list)      # [1, 2, 3]

Also, note that it is an extremely bad idea to call one of your lists list as list is a keyword in Python. (It is used to construct new lists, e.g. list(range(10)) creates a list [0, 1, ..., 9]).

Answered By: FIlip Müller
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.