I have a list of lists in python with two float values in each list. I would like to iterate on list of lists, but I desire to store first list in resulting list and compare every next list with the previous one and if it is different from the previous list then I again need to store that list in the resulting list.
list_of_lists = [[0.9953129999999999, 13.625421], [0.9953129999999999, 13.625421],[0.9953129999999999, 13.625421], [0.9953129999999999, 13.625421], [0.9953129999999999, 13.625421], [1.6215, 3.26078], [1.6215, 3.26078], [1.6215, 3.26078], [1.6215, 3.26078], [1.0, 12.25871], [1.0, 12.25871], [1.0, 12.25871], [1.0, 12.25871], [1.0, 12.25871], [1.0, 12.25871], [1.0, 12.25871], [1.0, 12.25871], [1.0, 12.25871], [1.0, 12.25871], [1.0, 12.25871], [1.0, 12.25871], [1.9050619999999998, 0.011995], [1.9050619999999998, 0.011995], [1.9050619999999998, 0.011995], [1.9050619999999998, 0.011995],[1.7293490000000002, 1.5182360000000001]]
My initial approach is like this;
resulting_list = []
resulting_list.insert(0,list_of_list[0])
print (resulting_list)
for index, rows in list_of_lists:
if ...
Thanks in advance!
You are almost there, you put the first sublist in resulting lists. and then iterate over the remaining items, you can then check if the current sublist matches the last sublist in resulting list, if it doesnt then ad this sublist.
list_of_lists = [[0.9953129999999999, 13.625421], [0.9953129999999999, 13.625421], [0.9953129999999999, 13.625421],
[0.9953129999999999, 13.625421], [0.9953129999999999, 13.625421], [1.6215, 3.26078], [1.6215, 3.26078],
[1.6215, 3.26078], [1.6215, 3.26078], [1.0, 12.25871], [1.0, 12.25871], [1.0, 12.25871],
[1.0, 12.25871], [1.0, 12.25871], [1.0, 12.25871], [1.0, 12.25871], [1.0, 12.25871], [1.0, 12.25871],
[1.0, 12.25871], [1.0, 12.25871], [1.0, 12.25871], [1.9050619999999998, 0.011995],
[1.9050619999999998, 0.011995], [1.9050619999999998, 0.011995], [1.9050619999999998, 0.011995],
[1.7293490000000002, 1.5182360000000001]]
resulting_list = [list_of_lists[0]]
for sub_list in list_of_lists[1:]:
if sub_list != resulting_list[-1]:
resulting_list.append(sub_list)
print(resulting_list)
OUTPUT
[[0.9953129999999999, 13.625421], [1.6215, 3.26078], [1.0, 12.25871], [1.9050619999999998, 0.011995], [1.7293490000000002, 1.5182360000000001]]