3

With numpy array, you can check a specific column with slicing, i.e. array[:, 0]. For a list, checking whether a 2D element is inside is as simple as ["one", "two"] in some_list. However, just looking if "one" is present requires one to iterate through the elements, i.e. ["one" == item[0] for item in some_list].

I largely prefer numpy array except that my array needs to be modified (adding at the end and removing values). I like to work with list because they are very simple to work with. I am considering dataframe but I feel like I should be able to find some clever and efficient way to this kind of operation using a list.

mooder
  • 341
  • 2
  • 8
  • 1
    Could you add an example of the 2D list and the value you're looking for? Your example talks about looking for a "2D element" and then shows a 1D list (`["one", "two"]`) and it's not clear what the larger `some_list` looks like. – Samwise Jun 27 '20 at 21:08
  • Are you just trying to test whether or not a given target exists in a 2D list? – kaya3 Jun 27 '20 at 21:11
  • I think that is the best you can do with lists. If you need to frequently perform checks to see if a value exists in a 2D list, then it might be worth to maintain a dictionary of all the values you inserted. If such searches are rare, then I guess this is the best with lists. – pecey Jun 27 '20 at 21:12
  • There is no more efficient way... Both arrays and lists require linear search for this sort of thing. Unless your data is sorted. – juanpa.arrivillaga Jun 27 '20 at 21:22
  • What are the dimensions of the array? –  Jun 27 '20 at 21:59

2 Answers2

6

To check whether item exists at any position in a 2D list list_of_lists, you can do

any(item in sublist for sublist in list_of_lists)

Note, unlike the list-flattening idea of another answer, this solution doesn't require any extra memory to be used.

Dan R
  • 1,412
  • 11
  • 21
-1

Not sure if this is what you're asking, but if all you want is to know if an element exists inside a list of lists l, you can flatten l and use the operator in as below:

>> l = [["one", "two"], ["three", "four"]]
>> one_is_in_l = "one" in [item for sublist in l for item in sublist]
>> print(one_is_in_l)
True

You can also use numpy just to do the check with:

>> l = [["one", "two"], ["three", "four"]
>> one_is_in_l = "one" in np.array(l)
>> print(one_is_in_l)
True
  • Or I could transform my numpy array into a new one with extra/less values which sound incredibly inefficient... Your first solution is equivalent to mine with: any(["one" == item[0] for item in some_list]) – mooder Jun 27 '20 at 21:29
  • 1
    It is equivalent but using the operator "in" should be more efficient than doing the manual iteration. Another thing you can do is to create create a new list of lists with elements to be added to the original one keeping track of their intended indexes. At the end of the process you can then assemble the new matrix with numpy. – Bernardo Trindade Jun 27 '20 at 21:36
  • Note that this is very inefficient. It creates the entire flattened list, even if the first element satisfies the condition. – MisterMiyagi Jun 27 '20 at 21:39
  • It sue is inefficient, though it's short and it shouldn't matter if it's not in the overall code's critical path. Maybe mooder could provide more context? – Bernardo Trindade Jun 27 '20 at 21:47