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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:12:52+00:00 2026-06-01T03:12:52+00:00

Alright well I am trying to create a dictionary from a text file so

  • 0

Alright well I am trying to create a dictionary from a text file so the key is a single lowercase character and each value is a list of the words from the file that start with that letter.

The text file containts one lowercase word per line eg:

airport
bathroom
boss
bottle
elephant

Output:

words = {'a': ['airport'], 'b': ['bathroom', 'boss', 'bottle'], 'e':['elephant']}

Havent got alot done really, just confused how I would get the first index from each line and set it as the key and append the values. would really appreatiate if someone can help me get sarted.

words = {}

for line in infile:
  line = line.strip() # not sure if this line is correct
  • 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-01T03:12:53+00:00Added an answer on June 1, 2026 at 3:12 am

    So let’s examine your example:

    words = {}
    for line in infile:
      line = line.strip()
    

    This looks good for a beginning. Now you want to do something with the line. Probably you’ll need the first character, which you can access through line[0]:

      first = line[0]
    

    Then you want to check whether the letter is already in the dict. If not, you can add a new, empty list:

      if first not in words:
        words[first] = []
    

    Then you can append the word to that list:

      words[first].append(line)
    

    And you’re done!

    If the lines are already sorted like in your example file, you can also make use of itertools.groupby, which is a bit more sophisticated:

    from itertools import groupby
    from operator import itemgetter
    
    with open('infile.txt', 'r') as f:
      words = { k:map(str.strip, g) for k, g in groupby(f, key=itemgetter(0)) }
    

    You can also sort the lines first, which makes this method generally applicable:

    groupby(sorted(f), ...)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright, so basically what I'm trying to do is use the value (which in
I wrote a program that is based completely on a single text file: I
Alright, switching from working Hpricot to Libxml-ruby due to speed and well the disappearance
Alright, i dont know how to explain it well.. but i have a switch
Alright, here is my problem i'm trying to solve. I have an index page
Alright, I thought I understood generics pretty well, but for some reason I can't
Alright well, I connect to 3 different ip's when I run this script. It
Alright, well this is the deal. I have a page, and then it loads
Alright, I'm trying to make it so that someone can vote for a server
Alright, Question #1: well i have a richtextbox, and id like to add emotions

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.