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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T12:38:16+00:00 2026-06-02T12:38:16+00:00

I have a Python list containing hundreds of thousands of words. The words appear

  • 0

I have a Python list containing hundreds of thousands of words. The words appear in the order they are in the text.

I’m looking to create a dictionary of each word associated with a string containing that word with 2 (say) words that appear before and after it.

For example the list: “This” “is” “an” “example” “sentence”

Should become the dictionary:

"This" = "This is an"
"is" = "This is an example"
"an" = "This is an example sentence"
"example" = "is an example sentence"
"sentence" = "an example sentence"

Something like:

WordsInContext = Dict()
ContextSize = 2
wIndex = 0
for w in Words:
    WordsInContext.update(w = ' '.join(Words[wIndex-ContextSize:wIndex+ContextSize]))
    wIndex = wIndex + 1

This may contain a few syntax errors, but even if those were corrected, I’m sure it would be a hideously inefficient way of doing this.

Can someone suggest a more optimized method please?

  • 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-06-02T12:38:18+00:00Added an answer on June 2, 2026 at 12:38 pm

    My suggestion:

    words = ["This", "is", "an", "example", "sentence" ]
    
    dict = {}
    
    // insert 2 items at front/back to avoid
    // additional conditions in the for loop
    words.insert(0, None)
    words.insert(0, None)
    words.append(None)
    words.append(None)
    
    for i in range(len(words)-4):   
        dict[ words[i+2] ] = [w for w in words[i:i+5] if w]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have passed Python list to template like l=['text','there is no salary','I need alcohol','and
In Python I have a list of n lists, each with a variable number
I have a list pts containing N points (Python floats). I wish to construct
I have two sets of numbers, each in a list in my Python script.
I have a Python class containing a list, to which I append() values. If
I have text file containing a list of 16 bit hex numbers (e.g. '61C7393AA9B3474DB081C7B7CCE1C545')
i have an issue i could use some help with, i have python list
I have Python nested list that I'm trying to organize and eventually count number
I have a python list which contains dictionaries and I want to make a
I have a Python list that I'm supplying to the template: {'error_name':'Please enter a

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.