16 AnswersNew Answer
Irsa There are many ways to solve this problem Here I'm giving you 2 a=["mr a","mr b","mr c"] print(a[:-1]) #or a=["mr a","mr b","mr c"] b=[i for i in a if i!="mr c"] print(b) I hope it helped 🙂
꧁༒Rishabh༒꧂ his question is unclear : - he wants maybe to remove the last element of the list (in that case you're right) - he wants to delete a specific element by comparing its value (in that case I'm right)
Théophile yeah right
thanks every one 😀
My interpretation suggests that Irsa is not seeking a way to handle exception but to leave out a specific element in a list. Interesting responses and views exhibited. What I can say is that, it depends on what you are comfortable with. - You can slice and leave the last item; - You can loop (for loop) through the list; - You can remove or delete the unwanted element and print the rest [:-1]; - You can check the element in the list and use continue (for loop) when the element is equal to 'mr c'; - You can use a lambda function and filter; Above all, it is crucial to consider writing a reusable code.
names = ['mr a','mr b','mr c'] print(list(filter(lambda x: x!='mr c',names))) --- OUTPUT: ['mr a','mr b']
Welcome Irsa 👍
To include except you have to use try. But I didn't understood your concept.They are mostly used for handling errors. Please clarify it.. Then, I'll try to help you
except keyword is used in exception handling : try: 1/0 except ZeroDivisionError as e: # handle the exception You want to print all objects in 'names' excepted 'mr c'. To achieve that, the easiest way at your level is creating a new list and display it. There are other ways to do this, using the built-in 'filter' function, for instance.
Théophile If this is his question, then easiest way might be list slices..
We can also get that result excluding 'mr.c' in this way.... print(names) print(names)
It's u can use the remove keyword for example friends = ["Batman","Pooboy"] friends.remove ("Pooboy") print (friends)
You can use for loop to print all the elements you want, using if statement to exclude the ones you don't want
siddiraju Manaswi can be many name
Lucky Sibanda yeah is so the idea is it ,get out the element 'mr c' of the list