Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to solve what should be an easy problem:

Python
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:

Python
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.
Posted
Updated 14-Oct-18 6:47am
v2
Comments
Richard MacCutchan 14-Oct-18 10:23am    
I think the problem is (although I cannot be sure of the correct indentation) that you are creating a new list from the value of z each time. So at the end of the program you only have one value in z to be printed. You need to start with an empty list and then add each item to z if it is not a 3. Alternatively you could print each number as you process it.

1 solution

Python
userInput = input("list element seperated by space: ")
nums = userInput.split()
for x in list(nums):
    y = int(x) * 3
    if y != 3:
        print(y, end=' ')
print("")


or using a list comprehension
Python
userInput = input("list element seperated by space: ")
nums = userInput.split()
products = [int(x) * 3 for x in nums if x != "1"]
print(products)
 
Share this answer
 
Comments
Member 14019160 14-Oct-18 13:58pm    
You got there before I thought it'd be a good idea to refresh this page! Silly me! But I'd been spending my time youtubing list comprehnsion and managed to come up with:

userInput = input("list element seperated by space: ")
nums = userInput.split()
mapped = [str((int(chr) * 3)) for chr in nums]
result = [chr for chr in mapped if not chr.endswith("3")]
print(mapped)
print(result)

Which is pretty much what you provided.

Thanks so much for the solid answer! :)
Richard MacCutchan 15-Oct-18 3:54am    
Well done solving it yourself, and happy to help.
There is some more useful details on lists at 5. Data Structures — Python 3.4.9 documentation[^].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900