In Python, I want to add a number to each element in a list, but I get [[0,1],2] instead of [0,1,2]. How to fix this?

Solution 1:

You need to unpack the items in b when creating the new list

listC.append([*b,i])

or if its easier to understand, add a new list with i to list b

listC.append(b + [i])

Solution 2:

If you want to keep only unique lists (unique in the sense that the exact set of its elements are not found elsewhere in listC) in listC, then you might want to consider replacing

listC.append([b,i])

with

new = set([*b, i])
if new not in listC:
    listC.append(new)

Then the final output becomes:

>>> listC = [list(c) for c in listC]
[[0, 1, 2], [0, 1, 3], [1, 2, 3]]