Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5973129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:45:24+00:00 2026-05-22T20:45:24+00:00

I have, in Wolfram Mathematica 8.0, a nested list like nList = {{a,b},{f,g},{n,o}} and

  • 0

I have, in Wolfram Mathematica 8.0, a nested list like

nList = {{a,b},{f,g},{n,o}}

and a normal list like

lList = {a,b,c,k,m,n,o,z}

and i want to check if all the sublists in nList are in lList (in the example a,b and n,o are there but not f,g)

I’ve done it using For[,,,] and using index… can someone enlighten me in using functions like Map/Thread/Select to do it in one pass.

Edit: If nList contains a,b, lList must contain a,b and not a,c,b or b,a or b,c,a

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:45:24+00:00Added an answer on May 22, 2026 at 8:45 pm

    Assuming that you don’t care about element ordering, here is one way:

    In[20]:= Complement[Flatten[nList],lList] ==={}
    
    Out[20]= False
    

    EDIT

    If the order matters, then here is one way:

    In[29]:= And@@(MatchQ[lList,{___,PatternSequence[##],___}]&@@@nList)
    
    Out[29]= False
    

    For large number of sub-lists, this may be faster:

    In[34]:= 
    Union[ReplaceList[lList,
           {___,x:Alternatives@@(PatternSequence@@@nList),___}:>{x}]]===Union[nList]
    
    Out[34]= False
    

    This works as follows: ReplaceList is a very nice but often ignored command which returns a list of all possible expressions which could be obtained with the pattern-matcher trying to apply the rules in all possible ways to an expression. This is in contrast with the way the pattern-matcher usually works, where it stops upon the first successful match. The PatternSequence is a relatively new addition to the Mathematica pattern language, which allows us to give an identity to a given sequence of expression, treating it as a pattern. This allowed us to construct the alternative pattern, so the resulting pattern is saying: the sequence of any sublist in any place in the main list is collected and put back to list braces, forming back the sublist. We get as many newly formed sublists as there are sequences of the original sublists in the larger list. If all sublists are present, then Union on the resulting list should be the same as Union of the original sublist list.

    Here are the benchmarks (I took a list of integers, and overlapping sublists generated by Partition):

    In[39]:= tst = Range[1000];
    
    In[41]:= sub = Partition[tst, 2, 1];
    
    In[43]:= 
    And @@ (MatchQ[tst, {___, PatternSequence[##], ___}] & @@@ sub) // Timing
    
    Out[43]= {3.094, True}
    
    In[45]:= 
    Union[ReplaceList[tst, {___,x : Alternatives @@ (PatternSequence @@@ sub), ___} 
         :> {x}]] ===  Union[sub] // Timing
    
    Out[45]= {0.11, True}
    

    Conceptually, the reason why the second method is faster is that it does its work in the single run through the list (performed internally by ReplaceList), while the first solution explicitly iterates through the big list for each sub-list.

    EDIT 2 – Performance

    If performance is really an issue, then the following code is yet much faster:

    And @@ (With[{part = Partition[lList, Length[#[[1]]], 1]},
     Complement[#, part] === {}] & /@SplitBy[SortBy[nList, Length], Length])
    

    For example, on our benchmarks:

    In[54]:= And@@(With[{part = Partition[tst,Length[#[[1]]],1]},
           Complement[#,part]==={}]&/@SplitBy[SortBy[sub,Length],Length])//Timing
    
    Out[54]= {0.,True}
    

    EDIT 3

    Per suggestion of @Mr.Wizard, the following performance improvement can be made:

    Scan[
     If[With[{part = Partition[lList, Length[#[[1]]], 1]},
       Complement[#, part] =!= {}], Return[False]] &,
     SplitBy[SortBy[nList, Length], Length]
    ] === Null
    

    Here, the as soon as we get a negative answer from sub-lists of a given length, sublists of other lengths will not be checked, since we already know that the answer is negative (False). If Scan completes without Return, it will return Null, which will mean that lList contains all of the sublists in nList.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with Wolfram Mathematica 8 and have the following problem. I have
Have you ever seen any of there error messages? -- SQL Server 2000 Could
**Have it working now. I forgot to populate the Array List. How embarrassing. I'm
Have a rather abstract question for you all. I'm looking at getting involved in
I'd like to know if there's a way that when using Matlab, instead of
I would like to plot the shifted logistic function as shown from Wolfram Alpha
Have a photography site that I want to prevent image copying from. How can
I've been using ToMatlab[], available at http://library.wolfram.com/infocenter/MathSource/577/ , to convert Mathematica expressions to Matlab
My Situation I have a N rectangles The rectangles all have the same shape
Have advancements in CPU design like dynamic instruction scheduling narrowed the performance gap between

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.