Reputation: 61
I am trying to write a function which outputs all possible combinations of char list with length and without any repeats like aa, bb etc.
I am now on this stage:
def sequences(char_list, n, lst = []):
if len(lst) == n:
print(lst)
else:
for i in range(len(char_list)):
temp_list = [char_list[j] for j in range(len(char_list)) if i != j]
sequences(temp_list, n, lst + [char_list[i]])
print(sequences(["a", "b", "c"], 2))
Output is correct but I have None at the end. I actually have no idea why.
['a', 'b']
['a', 'c']
['b', 'a']
['b', 'c']
['c', 'a']
['c', 'b']
None
And what is the best way to get strings in the output and not lists?
Upvotes: 0
Views: 42
Reputation: 44838
The function sequences
doesn't return anything (there's no return
statement anywhere in the code), so it'll automatically return None
. print(sequences(["a", "b", "c"], 2))
will execute this function and print its return value, outputting None
.
To get strings instead of lists, concatenate all the strings in the list like this:
print(''.join(lst))
Upvotes: 1
Reputation: 2370
Every function has an implicit return None
at the end of it. On the last line of your code, you asked Python the print the output of sequences
, which is None
since no return value was specified.
Upvotes: 0