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 6917977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:48:00+00:00 2026-05-27T09:48:00+00:00

How to match exact string/word while searching a list. I have tried, but its

  • 0

How to match exact string/word while searching a list. I have tried, but its not correct. below I have given the sample list, my code and the test results

list = ['Hi, friend', 'can you help me?']

my code

dic=dict()
for item in list:
    for word in item.split():
        dic.setdefault(word, list()).append(item)

print dic.get(s)

test results:

s = "can" ~ expected output: 'can you help me?' ~ output I get: 'can you help me?'
s = "you" ~ expected output: *nothing* ~ output I get: 'can you help me?'
s = "Hi," ~ expected output: 'Hi, friend' ~ output I get: 'Hi, friend'
s = "friend" ~ expected output: *nothing* ~ output I get: 'Hi, friend'

My list contains 1500 strings. Anybody can help me??

  • 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-27T09:48:01+00:00Added an answer on May 27, 2026 at 9:48 am

    Looks like you need a map of sentences and their starting word, so you don’t need to map all words in that sentence but only the first one.

    from collections import defaultdict
    
    sentences = ['Hi, friend', 'can you help me?']
    
    start_sentence_map = defaultdict(list)
    for sentence in sentences:
        start = sentence.split()[0]
        start_sentence_map[start].append(sentence)
    
    for s in ["can", "you", "Hi,", "friend"]:
        print s,":",start_sentence_map.get(s)
    

    output:

    can : ['can you help me?']
    you : None
    Hi, : ['Hi, friend']
    friend : None
    

    Also note few things from the code above

    1. Don’t use name list as name of variable because python uses it for list class
    2. Use default dict which makes it easy to directly add entries to dictionary instead of first adding a default entry
    3. Better descriptive names instead of mylist, or dic
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to match exact string/word while searching a list. I have tried, but its
similar questions have been asked before but I cant find an exact match to
I've been using word boundaries to look up exact words in a string but
I am looking for a way to check if an exact string match exists
I'm trying to search for an exact match of a string in mysql. The
How do i find out if a string is containing the exact word i
How to exactly match a given string in a sentence. For example if the
Using the Sql Server 2008 how can you actually find an exact string match
I would like to find the count of Exact match of string Let suppose
How do I enable exact match from start of string using jquery autocomplete with

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.