Remove Special Characters From List Python
Last Updated : Jan 1, 2023
IN - Python
In this tutorial we will show you the solution of remove special characters from list Python, Python presents to us enormously helpful ways to hold data inside variables. A class of such variables are container variables, part of a group called Python Collections.
These are literally extremely useful ways to hold data and operate on them conveniently. One of these collections is called List.
A list can hold any kind of data like string, number, special character etc. It is denoted by square brackets [ ]. For e.g this is a list list1 = [1,2,’s’,’tom’,’%’,’1s3’]. The data type of a list is called list.
In this demonstration, we will rather show You how to remove special characters from a list.
Through the course of this demonstration You will understand the remove() and append() methods of a list, too.
Step By Step Guide On Remove Special Characters From List Python :-
# Raw list consisting of alphanumeric and special characters l1=[1,19,'s','a','%',5,'X',55000,'$$','dol','^&*?23231','%1','&2','&*Z','//e'] l2= for e in l1: if type(e)==int: l2.append(e) for x in l2: l1.remove(x) for y in l1: if (y>='a' and y<='z') or (y>='A' and y<='Z'): l2.append(y) print('\nThe cleaned list is:',l2)
- We will use VS Code for this example demonstration.
- Firstly, we declare a list l1 to hold our diverse data ranging from numbers, strings, special characters to various combinations of the same. We will get this list rid of special characters completely.
- Our logic or line of thought will be to keep numbers and strings. Yes. It sounds very plain but You will be amazed by the power of this simplicity.
- We will define an empty list l2 to contain data devoid of special characters.
- Now our logic implementation starts. We define a for loop intended to filter out integers from list l1 and add, rather append them into l2. For each item ‘e’ in list l1, if the data type of that item is integer, we take that item and put it inside list l2. This is done using list append() method, which literally means to add at the end. This is done as l2.append(e). In this way we go on checking and populating l2 until all items of list l1 are exhausted comparing. This checking is done using == operator, read double-equals-to, whose task is to compare two things.
- If you print the list l2 after this process, You will see all items were added successively thus living upto the explanation of ‘append’.
- Our next step in our logic is to filter only strings and alphabetical characters from the list l1 and append them into list l2. We do it by using another straightforward for loop, which says that for every item ‘x’ in list l2, remove that same item from list l1. Our list l2 had only integer numbers in the last step. So this would delete all integers from the list l1. This is done using remove() method of list and implemented as l1.remove(x).
- We ultimately have a list devoid of integers and containing only strings, special characters and combinations of the same. We wish to pluck out the strings and append them to list l2.
- We do this process by running a for loop which says that for every item ‘y’ in list l1, if it’s a character (put into logic as something which is between a-z, including a & z and A-Z, including A & Z), we append the same into list l2. Yes, python makes character comparison this simple!
- Now we print the final cleaned list l2. You can see that only numbers and strings exist. All special characters and their mutations have been wiped.
I hope this tutorial on remove special characters from list python helps you and the steps and method mentioned above are easy to follow and implement.