Python Forum

Full Version: I'm getting an AttributeError. Don't know how to go arround it.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
About the program.
I want the program to output all the possible results in a list. But I'm currently struggling removing the current quotes, commas, spaces and brackets from the output.

If the list contains 1 and A. I'm looking to get an output like the following:

11
1A
AA
A1

The current output without the AttributeError is:

('1', '1')
('1', 'A')
('A', 'A')
('A', '1')


Please find the code attached:
import itertools       
                       
blocks = 2             
List = ["1", "A"]
                       
List.replace(",", "")  

for result in itertools.product(List, repeat=blocks):
	print(str(result)) 
Here is the error:
Error:
List.replace(",", "") AttributeError: 'list' object has no attribute 'replace'
I'm using the Python 3.8.3 in Ubuntu with Pycharm.

I've search online "AttributeError: 'list' object has no attribute 'replace'" but with my limited knowledge in python and overall programing I didn't manage to find a clear solution.

Thank you for your time.
Remove line 6,as just need to use the List in the loop,then .join() together the result.
import itertools

blocks = 2
List = ["1", "A"]
for result in itertools.product(List, repeat=blocks):
    print(''.join(result))
Output:
11 1A A1 AA
you need to join the elements of result
import itertools       
                        
blocks = 2             
my_list = ["1", "A"]
 
for result in itertools.product(my_list, repeat=blocks):
    print(''.join(result))
results is a tuple you can unpack the two items and display them with string formating
import itertools

blocks = 2
my_list = ["1", "A"]

for first, second in itertools.product(my_list, repeat=blocks):
    print(f'{first}{second}')
Output:
11 1A A1 AA
:-) three answers within a minute
Impressive response time!
@snippsat, @buran and @Yoriz. Thank you very much for helping me solve the problem.