0

I'm new in python and I have a simple question how could I delete the first "nan" of this list but maintain the second in case the value is duplicated.

for example:

my_list = ['experiencia: 4',
           'nan',
          'experiencia: 2',
           'nan',
           'nan',
          'experiencia: 10',
           'nan',
          'experiencia: 2',
           'nan',
          'experiencia: 3',
           'nan',
           'nan',
           'nan',
           'experiencia: 9',
           'nan',
           'experiencia: 10']

and the output I want to obtain is the following:

 my_list = ['experiencia: 4',
          'experiencia: 2',
           'nan',
          'experiencia: 10',
          'experiencia: 2',
          'experiencia: 3',
           'nan',
           'nan',
           'experiencia: 9',
           'experiencia: 10']
cristobalvch
  • 103
  • 6
  • Have you tried coding it up yet? sometimes when it's tricky to figure out how to go about implementing something, it helps to break it up into a flow chart of individual steps. – Aaron Sep 05 '20 at 22:08
  • I also would point out, it is often easier to build a new list rather than trying to modify an existing one in-place. – Aaron Sep 05 '20 at 22:10

4 Answers4

2

You can ... get creative:

Group the whole list by its contents and disassemle the grouping again. If the key is a "nan" you need to use one less then the groupings value has elements - for non-"nan" use the grouping value as is. Lastly, remove empty "nan" - groupings (key = "nan", value = ["nan"] gets reduced to [] due to subtracting one):

my_list = ['experiencia: 4',
           'nan',
          'experiencia: 2',
           'nan',
           'nan',
          'experiencia: 10',
           'nan',
          'experiencia: 2',
           'nan',
          'experiencia: 3',
           'nan',
           'nan',
           'nan',
           'experiencia: 9',
           'nan',
           'experiencia: 10']

from itertools import groupby


result = [x for y in (list(amount) if value != "nan" else ["nan"] * (len(list(amount))-1)
                  for value, amount in groupby(my_list)) for x in y if x]
print(result)

Output (reformatted):

['experiencia: 4', 
 'experiencia: 2', 
 'nan', 
 'experiencia: 10', 
 'experiencia: 2',
 'experiencia: 3', 
 'nan', 
 'nan', 
 'experiencia: 9', 
 'experiencia: 10'] 

Readup:

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • thanks a lot! i was using the Pandas Series and dropping the index using a similar criteria, but your way to do it is better than mine, thanks! – cristobalvch Sep 05 '20 at 22:32
0

Create a list for the indices you wish to do delete and delete them using the pop method

delete_list = [1,2,4,5,6,9]
newlist = ''
for i in delete_list:
   my_list.pop(i)
print (my_list)

Result

['experiencia: 4', 'experiencia: 2', 'nan', 'experiencia: 10', 'experiencia: 2', 'experiencia: 3', 'nan', 'nan', 'experiencia: 9', 'experiencia: 10']

Note also, that i have shifted my deletion pattern to suit the index adjustment after every deletion

Seyi Daniel
  • 2,259
  • 2
  • 8
  • 18
-1
my_list.remove('nan')

will delete only the first 'nan'

Hadrian
  • 917
  • 5
  • 10
-1

This will also give you the desired output:

from itertools import groupby
from itertools import chain
my_list = [*chain.from_iterable([[k] * (len([*v]) - 1) if k == "nan" else [k] for k, v in groupby(my_list)])]
Brian Larsen
  • 612
  • 8
  • 9