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

  • Home
  • SEARCH
  • 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 6999083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:29:43+00:00 2026-05-27T20:29:43+00:00

I have a question that is similar to this question , but mine concerns

  • 0

I have a question that is similar to this question, but mine concerns multidimentional lists like this:

myList = [['abc', 'abc-321'], ['def', '789-abc'], ['xyz', 'xyz-123']]
newList = []

I want to search this myList for a certain word/phrase, and if there are matches, the entire sublist to be appended to newList, with a way to control with elements are searched

For example, If I search for the the tern ‘abc’ in the first and second element of each sublist, newList should be (two matches):

[['abc', 'abc-321'], ['def', '789-abc']]

but if only the first element is to be searched, newList should be (only one match):

[['abc', 'abc-402']]

How can I do both types of searches in the most efficient way? Please consider this: The list to be searched contains around a thousand sublists and the text to be searched is about 1-2 paragraphs on average.

  • 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-27T20:29:43+00:00Added an answer on May 27, 2026 at 8:29 pm

    Try This

    def foo(myList,key,first=True):
        if first: #First Element Search
            return [x for x in myList if key in x]
        else: #Search Both Element
            return [x for x in myList if key in x or key in x[1]]
    
    >>> foo(myList,'abc',0)
    [['abc', 'abc-321']]
    >>> foo(myList,'abc',1)
    [['abc', 'abc-321'], ['def', '789-abc']]
    

    Note: This simply utilized List Comprehension. The only thing to note is the comparison. Comparing two string with in will try to match the first string any where in the second string. Comparing a string with a list, it will try to match the first string anywhere in the first element of the string.

    A simpler implementation

    def foo(myList,key,first=True):
        return [x for x in myList if key in x or not first and key in x[1]]
    
    >>> foo(myList,'abc')
    [['abc', 'abc-321']]
    >>> foo(myList,'abc',first=False)
    [['abc', 'abc-321'], ['def', '789-abc']]
    >>> foo(myList,'abc',first=True)
    [['abc', 'abc-321']]    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Similar to this question I have a custom subclass of UITableViewCell that has a
Similar to this question: link However I have already mastered that. My problem is
I know that this is a repeated question. I have found very similar questions
That's a question really similar to this previous post of mine. I need to
I found a very similar question to mine, but not exactly the same. This
Hey a similar question has been asked before, but none that answers mine exactly.
I think I have question that looks similar to many old questions but it
This question is similar to a previous question of mine , but I formulated
There's a similar question to this on this site but being that I cannot
I have a noob-question, that might be similar to Dynamically create multiple instances of

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.