BLACK FRIDAY EXTRA SAVINGS EVENT - EXTENDED
START FREE

Dictionaries practice #6 Removing rows : Alternative solution

Screen Link:

My Code:

def remove_rows(dic, indices):
    for index in indices:
        for key in dic:
            del dic[key][index]  
    return dic

It’s straightforward, but here is a little breakdown:

  1. The 2nd argument indices is a list containing the numbers of the rows to be removed. The outer loop iterates through this list.
  2. For each index / row number, the inner loop checks if that index is a key in the inner dictionary. If it is, it removes the key/value pairs, i.e. removes the row.

The row removal is done by the del keyword.

3 Likes

There we go. I came up with the same solution. Dict and List comprehensions get really unreadable to me pretty quickly. Here’s my solution, it’s basically the same thing. I also ran timeit to see if it’s any faster. And it totally is.

from numpy import nan
import timeit


testcode_1 = ''' 

def remove_rows(d, index_list):
    for i in index_list:
        for k, v in d.items():
            if i in v:
                del v[i]
    return d

'''
print(timeit.repeat(stmt=testcode_1, setup=import_module))


testcode_2 = ''' 
def remove_keys(d, keys_to_remove):
    return {k:v for k,v in d.items() if k not in keys_to_remove}

def remove_rows(d, rows):
    return {col:remove_keys(value, rows) for col, value in d.items()}
'''
print(timeit.repeat(stmt=testcode_2, setup=import_module))


d = {
    'method': {160: 'Transit',
               506: 'Radial Velocity',
               943: 'Pulsar Timing',
               225: 'Radial Velocity',
               741: 'Transit'},
    'orbital_period': {160: 2.91626,
                       506: 4.6455,
                       943: 98.2114,
                       225: 1313.0,
                       741: 42.8961},
    'mass': {160: nan,
             506: 0.013000000000000001,
             943: nan,
             225: 0.63,
             741: nan},
    'distance': {160: 214.0,
                 506: 24.05,
                 943: nan,
                 225: 56.34,
                 741: nan}
}


for col,val in remove_rows(d, [506, 225]).items():
    print(col, val, sep="\n")

Here’s the output

[0.07418148400029168, 0.0731560590211302, 0.07316351297777146, 0.07949136500246823, 0.09211084002163261]
[0.21529960603220388, 0.2346322549856268, 0.16323177801677957, 0.16936843999428675, 0.20332421798957512]