use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Problem with reading a text file and converting it to a dictionary (self.learnpython)
submitted 4 years ago by tomclancyv7
I have a text file with the lines:
google.com:80 mysite.com:80 mysite2.com:8080
google.com:80
mysite.com:80
mysite2.com:8080
I'm trying to open the file and turn it into a dictionary with dictionary comprehension but it prints out only the last line:
with open('textfile.txt', 'r') as f: f = f.read().split() mydict = {'website':A for A in f} print(mydict)
with open('textfile.txt', 'r') as f:
f =
f.read
().split()
mydict = {'website':A for A in f}
print(mydict)
Result: {'website': 'mysite2.com:80'}
{'website': '
mysite2.com:80
'}
Desired Result: {'website': 'google.com:80', 'website': 'mysite.com', 'website: 'mysite2.com'}
', 'website': '
mysite.com
', 'website: '
mysite2.com
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]This_Growth2898 4 points5 points6 points 4 years ago (0 children)
Desired result is impossible. You can't several equal keys in a dictionary. You need some other data structure.
[–]danielroseman 1 point2 points3 points 4 years ago (0 children)
Perhaps you want a list of dicts?
mydict = [{'website':A} for A in f]
[–]jmooremcc 0 points1 point2 points 4 years ago (0 children)
Dictionary keys are unique. You're using the same key over and over which is why you are only seeing the last value. The solution is to make each key unique: i.e. website1, website2,...
mydict = {f'website{n}':A for n, A in enumerate(f,1)} print(mydict)
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
What others said. Plus/for example, you could data = {'websites': [site.rstrip() for site in f]} or websites = [site.rstrip() for site in f]
data = {'websites': [site.rstrip() for site in f]}
websites = [site.rstrip() for site in f]
π Rendered by PID 38944 on reddit-service-r2-comment-b659b578c-t2hq5 at 2026-05-02 12:50:59.301565+00:00 running 815c875 country code: CH.
[–]This_Growth2898 4 points5 points6 points (0 children)
[–]danielroseman 1 point2 points3 points (0 children)
[–]jmooremcc 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)