I'm trying to solve what should be an easy problem:
userInput = input("list element seperated by space: ")
nums = userInput.split()
for x in list(nums):
y = int(x) * 3
z = list(str(y))
while "3" in z:
z.remove("3")
else:
print(z)
With the above code; logically, all occurrences of "3" should be removed from the list. This is not functioning as desired however. Could someone explain why & how to fix?
I want to multiply individual elements of user input by 3 and then remove all instances of 3 from the list and print off the output.
I've also tried while True and using i += 1 counts and stuff but just can't seem to get this working or my head round it. Appreciate the help.
input - desired output - currently outputting
1 2 3 - 6 9 - 9
0 - 0 - 0
2 6 11 - 6 18 - []
I found out that what I should be using is list comprehension. So I did some research on that and have been able to get them to function, but still not as desired:
p = input("list element seperated by space: ")
nums = p.split()
mp = [num for num in nums * 3]
print(mp)
for x in list(nums):
y = int(x) * 3
z = str(y)
print(z)
c = [z]
filtered = [chr for chr in c if c != "3"]
print(filtered)
for n in list(z):
negate = [string for string in n if n != "3"]
print(negate)
The output remains the same. I don't understand why and how I can get around this?
Why is it so hard to scan a list for a string and remove it from the list?!? :(
Truthfully appreciate the help as I feel like desk-facing-palming-crying haha :P
What I have tried:
Stackoverflow, youtube, python documentation.