Nima Geran
Nima Geran

Reputation: 125

eliminating empty elements in a list with python

i have a list as shown below and i'm fairly new to python language . i want to eliminate those five empty elements in my list ( one is the first element (before '0' and four of them is after the last element ('1')) could you please how could i do this ? Thanx

['', '0.', '10.E-03', '12.5E-03', '15.E-03', '18.75E-03', '22.5E-03', '26.25E-03', '30.E-03', '33.75E-03', '37.5E-03', '43.125E-03', '51.5625E-03', '61.5625E-03', '71.5625E-03', '81.5625E-03', '91.5625E-03', '101.563E-03', '111.563E-03', '121.563E-03', '131.563E-03', '141.563E-03', '151.563E-03', '161.563E-03', '171.563E-03', '181.563E-03', '191.563E-03', '201.563E-03', '211.563E-03', '221.563E-03', '231.563E-03', '241.563E-03', '251.563E-03', '261.563E-03', '271.563E-03', '281.563E-03', '291.563E-03', '301.563E-03', '311.563E-03', '321.563E-03', '331.563E-03', '341.563E-03', '351.563E-03', '361.563E-03', '371.563E-03', '381.563E-03', '391.563E-03', '401.563E-03', '411.563E-03', '421.563E-03', '431.563E-03', '441.563E-03', '451.563E-03', '461.563E-03', '471.563E-03', '481.563E-03', '491.563E-03', '501.562E-03', '511.563E-03', '521.563E-03', '531.563E-03', '541.563E-03', '551.563E-03', '561.563E-03', '571.563E-03', '581.563E-03', '591.563E-03', '601.563E-03', '611.563E-03', '621.563E-03', '631.563E-03', '641.563E-03', '651.563E-03', '661.563E-03', '671.563E-03', '681.563E-03', '691.563E-03', '701.563E-03', '711.563E-03', '721.563E-03', '731.563E-03', '741.563E-03', '751.563E-03', '761.563E-03', '771.563E-03', '781.563E-03', '791.563E-03', '801.563E-03', '811.563E-03', '821.563E-03', '831.563E-03', '841.563E-03', '851.563E-03', '861.563E-03', '871.563E-03', '881.563E-03', '891.563E-03', '901.563E-03', '911.563E-03', '921.563E-03', '931.563E-03', '941.563E-03', '951.563E-03', '961.563E-03', '971.563E-03', '981.563E-03', '991.563E-03', '1.', '', '', '', '']

Upvotes: 2

Views: 2850

Answers (3)

Mr. Xcoder
Mr. Xcoder

Reputation: 4795

There are a lot of ways you can accomplish that. To illustrate the topic better, I've benchmarked all the methods, for you to see how efficient each of those are. I strongly recommend using methods 1 and 2. Here are the examples, ordered by their execution time:

1. Filtering the list (recommended)

myList = list(filter(None, myList))

Average execution time (measured with timeit()): 0.037 - 0.045 s


2. List Comprehension

myList = [x for x in myList if x]

Average execution time: 0.055 - 0.065 s


3. Removing '' n times, were n is the number of '' in the list

c = myList.count('')
for i in range(c):
    myList.remove('')

Average execution time: 0.088 - 0.1 s

Note: This is not the best way, you should use the recommended method


4. Removing '' while possible

Note: this method is inspired by this answer and isn't really the best way to do this

while True:
    try: myList.remove('')
    except ValueError: break

Average execution time: 0.097 - 0.11 s

Upvotes: 2

shizhz
shizhz

Reputation: 12491

You can either use list comprehension:

[x  for x in original_list if x != ''] # To only remove empty string

Or filter:

filter(lambda x: x != '', original_list) # To only remove empty string

Upvotes: 0

niraj
niraj

Reputation: 18208

my_list = ['', '0.', '10.E-03', '12.5E-03', '15.E-03', '18.75E-03', '22.5E-03', '26.25E-03', '30.E-03', '33.75E-03', '37.5E-03', '43.125E-03', '51.5625E-03', '61.5625E-03', '71.5625E-03', '81.5625E-03', '91.5625E-03', '101.563E-03', '111.563E-03', '121.563E-03', '131.563E-03', '141.563E-03', '151.563E-03', '161.563E-03', '171.563E-03', '181.563E-03', '191.563E-03', '201.563E-03', '211.563E-03', '221.563E-03', '231.563E-03', '241.563E-03', '251.563E-03', '261.563E-03', '271.563E-03', '281.563E-03', '291.563E-03', '301.563E-03', '311.563E-03', '321.563E-03', '331.563E-03', '341.563E-03', '351.563E-03', '361.563E-03', '371.563E-03', '381.563E-03', '391.563E-03', '401.563E-03', '411.563E-03', '421.563E-03', '431.563E-03', '441.563E-03', '451.563E-03', '461.563E-03', '471.563E-03', '481.563E-03', '491.563E-03', '501.562E-03', '511.563E-03', '521.563E-03', '531.563E-03', '541.563E-03', '551.563E-03', '561.563E-03', '571.563E-03', '581.563E-03', '591.563E-03', '601.563E-03', '611.563E-03', '621.563E-03', '631.563E-03', '641.563E-03', '651.563E-03', '661.563E-03', '671.563E-03', '681.563E-03', '691.563E-03', '701.563E-03', '711.563E-03', '721.563E-03', '731.563E-03', '741.563E-03', '751.563E-03', '761.563E-03', '771.563E-03', '781.563E-03', '791.563E-03', '801.563E-03', '811.563E-03', '821.563E-03', '831.563E-03', '841.563E-03', '851.563E-03', '861.563E-03', '871.563E-03', '881.563E-03', '891.563E-03', '901.563E-03', '911.563E-03', '921.563E-03', '931.563E-03', '941.563E-03', '951.563E-03', '961.563E-03', '971.563E-03', '981.563E-03', '991.563E-03', '1.', '', '', '', '']

You could also use:

my_list = [element for element in my_list if element]

# You could also use my_list = [element for element in my_list if not element == ''] 
# but checking for 'if element' works

Or, use filter as shown here:

my_list = list(filter(None, my_list))

Upvotes: 4

Related Questions