Home > Mobile >  printing list object with appended element not works - python
printing list object with appended element not works - python

Time:10-29

I have written a code to get list from tuple and append a number to it. I have read other posts about this, and know that list.append returns None, and None would be printed. But in my code, how can I append the new value? Thank you.

from itertools import combinations

steps = [1,2,3,4,5,6,7,8]
for i in range(7, 8):
    for subset in combinations(steps, i):
        tmp_subset = list(subset).append(1)
        print(tmp_subset)

CodePudding user response:

As you said in your question you know that list.append() returns None, therefore you are currently setting tmp_subset to the value returned by append(). So all you need to do is separate your line tmp_subset = list(subset).append(1) into:

tmp_subset = list(subset)
tmp_subset.append(1)

CodePudding user response:

Don't use append in a print, as returns None! I changed the code as follows and it works:

from itertools import combinations

steps = [1,2,3,4,5,6,7,8]
for i in range(7, 8):
    #print(i)
    for subset in combinations(steps, i):
        tmp_subset = list(subset)
        tmp_subset.append(1)
        print(tmp_subset)
  • Related