Right now I have a python list that looks like this:
['', '2015-10-21 00:00:03', 'jp/ja/fedex/inet/label/international' ]
[398798, '2015-10-21 00:00:10', 'us/en/fedex/inet/label/domestic' ]
[878787, '2015-10-21 00:00:16', 'us/en/fedex/fedexcares/home' ]
['87878', '', 'cn/zhs/fedex/inet/label/international']
['', '2015-10-21 00:00:18', '' ]
[5454, '2015-10-21 00:00:19', 'us/en/fedex/sameday/main tracking' ]
['', '2015-10-21 00:00:21', 'sg/en/fedex/inet/label/international' ]
This 2D list has 3 columns and more than ten thousands rows.
As you can see, some rows are missing elements at [0]
, and some are missing elements at [1]
, some are missing elements at [2]
.
Some have all three elements.
I need to delete all those rows, which do not have three elements.
That being said, as long as a row misses one element, it needs to be deleted.
So, for the list above, row[0][3][4][5][6]
need to be deleted.
After perform the delete function, the list should look like this:
[398798, '2015-10-21 00:00:10', 'us/en/fedex/inet/label/domestic' ]
[878787, '2015-10-21 00:00:16', 'us/en/fedex/fedexcares/home' ]
I'm thinking about this:
for i in range(len(D)): //D is the name of my list
if D[i][0] =='' or D[i][1]=='' or D[i][2] =='':
del D[i]
But this does not work, because as you are truncating the list, len(D)
is changing, you will not be able to iterate through the whole list.
I also thought about this:
for item in D:
if item[0]=='' or item[1]=='' or item[2] =='':
del item
This also does not at all.
I would really appreciate it if you could come up with something.