-1
list1 = ['A','B']
list2 = ['a','c']
list3 = ['x','y','z']
list4 = [['A','b','c'],['a','x'],['Y','Z'],['d','g']]

I want to check if all elements of list (list1, list2, list3) is contained in any of list in another bigger list (list4).

I want the comparison to be case insensitive.

To be sure, here list1 and list2 is in list4 but not list3. How can I do it?

On the other note, How would I know if a list is collection of list.

In other words, how can I distinguish if list is a collection of list of just list of elements, if I am not the one who is defining the lists.

3 Answers 3

1

First item - you want to do case-insensitive matching. The best way to do that is to convert everything to one case (upper or lower). So for each list, run

list1 = map(lambda x: x.lower(), list1)

That will convert your lists to lowercase. Let's assume you've done that.

Second, for a comparison of two "simple" lists (not-nested), you can simply say

if set(list1) < set(list2):

to compare if list1 is a subset of list2. In your example, it would be false.

Finally, if you want to check if a list is nested:

if ( type(list4[0]) == list) :

which in this case, would be true. Then, just iterate over the elements of list4 and do the set comparison above.

1

You can use lower() to make all elements of all lists to lowercase to achieve case-insensitivity.

def make_case_insensitive(lst):
    return [i.lower() for i in lst]

For example,

list1=make_case_insensitive(list1)

As, biggerlist is slightly different (contains list as element), you have to change the function slightly.

def make_bigger_list_caseinsensitive(bigger_list):
    return [[i.lower() for i in element] for element in bigger_list]

list4=make_bigger_list_caseinsensitive(list4)

Check if any element of the biggerlist is the superset of smaller set. Print Is in bigger list if condition satisfied, print not in biggger list otherwise. Make set from the list first.

print "Is in bigger list" if any(set(element).issuperset(set(list1)) for element in list4) else "not in biggger list"

To write it with slightly more readability, do:

if any(set(element).issuperset(set(list1)) for element in list4):
    print "Is in bigger list"
else:
    print "not in biggger list"

Finally,to check if nested list exists in biggerlist:

print any(type(element)==list for element in list4)
0

Using set is a good way.

list1 = ['A','B']
list2 = ['a','c']
list3 = ['x','y','z']
list4 = [['A','b','c'],['a','x'],['Y','Z'],['d','g']]

set1 = set(map(lambda s: s.lower(), list1))
set2 = set(map(lambda s: s.lower(), list2))
set3 = set(map(lambda s: s.lower(), list3))
set4 = map(lambda l: set(map(lambda s: s.lower(), l)), list4)

print(set1) # set(['a', 'b'])
print(set2) # set(['a', 'c'])
print(set3) # set(['y', 'x', 'z'])
print(set4) # [set(['a', 'c', 'b']), set(['a', 'x']), set(['y', 'z']), set(['d', 'g'])]

lor = lambda x, y: x or y

reduce(lor, map(lambda s: set1.issubset(s), set4)) # True
reduce(lor, map(lambda s: set2.issubset(s), set4)) # True
reduce(lor, map(lambda s: set3.issubset(s), set4)) # False
  • To do a case-insensitive string comparison, covert both strings to lowercase or uppercase.
  • To test all elements in list1 are contained in list4, use set.issubset.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.