I am a research scientist writing a custom class I'm calling MyList()
in Python 3.7, intended to add some additional methods for use on list type objects. I want these methods to be able to modify the list object in place, without having to redefine it or assign it to a new object name. For example, let's say I have already declared foo = MyList()
and populated it with some arbitrary data. My goal is to be able to do something like this:
>>> foo
[1, 1, '', 3, [2, 4, '', 4, 5], [], [6] ]
>>> type(foo)
__main__.MyList
>>> foo.flatten_the_list()
>>> foo.remove_empty_items_from_list()
>>> foo.remove_duplicate_items_from_list()
>>> foo.convert_to_list_items_to_strings()
>>> foo
['1', '2', '3', '4', '5', '6']
I have posted my code below, and so far some of the methods are working correctly. I've lumped in removing empty items from the list object, removing whitespace from items in the list object, and removing duplicate items from the list object into one method called MyList.cleanup()
that works well.
However, the same list comprehension that works just fine for MyList.cleanup()
, doesn't work for flattening the list, nor does it work for converting the type of each item in the list from integers to strings. My code so far:
class MyList(list):
# No def __init__() statement needed - inherits from list object
# MyList.convert_v2() WORKS without list comprehension
def convert_v2(self, dtype):
for i in range(len(self)):
self[i] = str(self[i])
return self
# MyList.convert_v1() DOESN'T WORK with list comprehension
def convert_v1(self, dtype):
self = [str(item) for item in self]
return self
# MyList.cleanup() WORKS with list comprehension
def cleanup(self):
# Remove empty items
self = [item for item in self if "" is not item]
# Remove duplicate items
self = list(dict.fromkeys(self))
# Remove whitespace (including \t and \n)
self = ["".join(str(item).split()) for item in self]
return self
# MyList.flatten() DOESN'T WORK with list comprehension
def flatten(self):
self = [item for sublist in self for item in sublist]
return self
This is what I get when I use the MyList.convert_v1() method (my first attempt at a method that converts the contents of a list to strings):
>>> bar
[1, 2, 3, 4, 5, 6]
>>> type(bar[0])
int
>>> bar.convert_v1()
['1', '2', '3', '4', '5', '6']
>>> bar
[1, 2, 3, 4, 5, 6]
>>> type(bar[0])
int
However, I had to stop using list comprehension to get the desired effect with MyList.convert_v2():
>>> bar
[1, 2, 3, 4, 5, 6]
>>> type(bar[0])
int
>>> bar.convert_v2()
['1', '2', '3', '4', '5', '6']
>>> bar
['1', '2', '3', '4', '5', '6']
>>> type(bar[0])
str
Why does MyList.convert_v2()
work as expected, when MyList.convert_v1()
does not? Outside of a class, I wouldn't expect either function to behave differently, but inside the class they do behave differently.
On a similar note, this is what I get for the MyList.flatten() method:
>>> baz
[[1, 2, 3, 4], [5, 6, 7, 8]]
>>> baz.flatten()
[1, 2, 3, 4, 5, 6, 7, 8]
>>> baz
[[1, 2, 3, 4], [5, 6, 7, 8]]
While the desired outcome is printed to the output as shown, the list object baz
isn't actually flattened. The list remains unchanged after the method is called. I need it to do this instead:
>>> baz
[[1, 2, 3, 4], [5, 6, 7, 8]]
>>> baz.flatten()
[1, 2, 3, 4, 5, 6, 7, 8]
>>> baz
[1, 2, 3, 4, 5, 6, 7, 8]
Why does list comprehension work just fine in the MyList.cleanup()
method, but not in the MyList.convert()
or MyList.flatten()
methods? I recognize that I am new to OOP and writing classes in general, so if I'm completely off base here, I look forward to learning what I could be doing differently.