What I want the code to do is to print the list containing numbers that are:
- product of 2- or 3-digit numbers
- palindrome
- in the range [101101, 1000000).
There should be no 5-digit numbers and some 6-digit numbers in the final list as they are less than 101101. But there still are some 5-digit numbers after processing. Why is that happening?
list1 = []
for i in range(100, 1000):
for j in range(100, 1000):
if str(i*j) == str(i*j)[::-1]: # checking for palindrome
list1.append(i*j)
list1 = list(set(list1)) # removing duplicates
print(sorted(list1))
# print(len(list1))
for ii in list1: # removing numbers, out of range
if ii < 101101 or ii >= 1000000:
list1.remove(ii)
print(sorted(list1))
# print(len(list1))
But when I use sets to remove the elements out of range, it works. The below given code does the job.
set1 = set(range(10000, 101102))
list1 = list(set(list1) - set1)
But I am not understanding, why the previous code fails to print the desired output?
Edit: As 1 of you suggested, yes it is a duplicate of this. My bad for not checking for the question in the existing bank.