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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:07:02+00:00 2026-06-06T07:07:02+00:00

I am writing a game in python in which I must periodically pull a

  • 0

I am writing a game in python in which I must periodically pull a random word from a list of words. When I prototyped my game I declared a word_list = ['cat','dog','rat','house'] of ten words at the top of one of my modules. I then use choice(word_list) to get a random word. However, I must must change this temporary hack into something more elegant because I need to increase the size of the word list to 5,000+ words. If I do this in my current module it will look ridiculous.

Should I put all of these words in a flat txt file, and then read from that file as I need words? If so, how would I best do that? Put each word an a separate line and then read one random line? I’m not sure what the most efficient way is.

  • 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-06T07:07:04+00:00Added an answer on June 6, 2026 at 7:07 am

    I’d put all of the words in a flat text file, one per line:

    cat
    dog
    ....
    

    and just load it in whenever you need it with the following one-liner:

    word_list = [word.rstrip() for word in open("words.txt","r")]
    

    See: http://docs.python.org/tutorial/datastructures.html#list-comprehensions

    This solution is a tad more elegant since it doesn’t depend on anything but built-in functions. No importing modules required.

    Be sure to cache it once it’s loaded, you don’t want to load the words from the file everytime you need a new word, though.

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

Sidebar

Related Questions

I'm writing a game in python with pygame and need to render text onto
I'm writing a multiplayer Flash game, and the server is written in Python and
I am writing a game which uses opengles. I have created my renderer class
I'm writing a game which uses a border layout with a JPanel using BorderLayout.CENTER.
I am writing a game in Python (with pygame) that requires me to generate
I am writing a dice game simulator in Python. I represent a roll by
I have the beginnings of a small multiplayer game that I'm writing in python
I am writing a game in which a Uiimageview needs to be 'spawned,' if
I'm writing a small sudoku game/solver in Linux using python with TUI (not GUI,
I'm writing a game in Python with the Pygame2 multimedia library, but I'm more

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.