Reputation: 130
If I have a nested list that looks like this:
bigstringlist = [['rob', 'bob', 'sam', 'angie'], ['jim', 'angie', 'tom', 'sam'], ['sam', 'mary', 'angie', 'sally']]
How do I iterate through this list and extract a list of names that appear in all the nested lists? i.e.:
finallist = ['sam', 'angie']
Would this be better accomplished by typecasting this nested list as a set?
Upvotes: 2
Views: 609
Reputation: 10162
A variation on singularity's solution, maybe a little faster:
bigstringiter = iter(bigstringlist)
reduce(set.intersection, bigstringiter, set(next(bigstringiter)))
Upvotes: 0