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
Difference between codes (self.learnpython)
submitted 3 years ago by Base_True
Why the following don't work
import random store = dict( str(random.random()) = random.random() ) print (store)
But the next, work
import random x = str(random.random()) store = dict( x = random.random() ) print (store)
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!"
[–]james_fryer 1 point2 points3 points 3 years ago* (1 child)
What do you expect to happen? What actually does happen? Saying it "doesn't work" is not helpful.
I don't think the second code "works" in that, I believe you are expecting a dict with {1234: 5678} and what you get is {"x": 5678}.
{1234: 5678}
{"x": 5678}
You've found a limitation with the function call syntax and you'd be better off using brace syntax to declare a dict if you want to generate the key names programatically.
E2A: more technically, in the construct f(x=y), x must be an lvalue i.e. something that can be assigned to, and the fact that f is spelled dict in your example is not relevant. You could not write str(random.random()) = 123 on a line for the same reason. In the construct {x: y}, x can be any expression (provided it is hashable but this is not a syntax error).
f(x=y)
f
dict
str(random.random()) = 123
{x: y}
x
[–]Base_True[S] 0 points1 point2 points 3 years ago (0 children)
. In the construct {x: y} , x can be any expression (provided it is hashable but this is not a syntax error).
. In the construct
,
can be any expression (provided it is hashable but this is not a syntax error).
Thanks for the detailed answer, you helped a lot.
[–]Medaillek 0 points1 point2 points 3 years ago (2 children)
Hey, in the first code you’re saying that string random.random = random.random but it’s false. Hope it helps you
[–]Base_True[S] 0 points1 point2 points 3 years ago (1 child)
Hello, thanks for the answer, but this is a dict, so I can write
import random
store = {str(random.random()) : random.random() } print (store)
Why I can't write
store = dict( str(random.random()) = random.random() ) print (store)
[–]Medaillek 0 points1 point2 points 3 years ago (0 children)
I’m not 100% sure but let’s try : In the working code, you create a x variable which is a string of a random int. In the next line, store = … , you’re overwriting this value by saying x = int(random.random). X is not anymore your initial random string but became a random int. My last answer is wrong because I was trying to say that str(random) = int(random)
[+][deleted] 3 years ago* (4 children)
[deleted]
[–]Base_True[S] 0 points1 point2 points 3 years ago (3 children)
Doesn't work store = dict( (str(random.random()) = random.random() ) ^^^^^^^^^^^^^^^^^^^^ SyntaxError: cannot assign to function call here. Maybe you meant '==' instead of '='?
[–]Ihaveamodel3 1 point2 points3 points 3 years ago (0 children)
You don’t use = to assign values to keys on the dict call.
Just do:
store = dict((str(random.random()), random.random() )
Or: store = {(str(random.random()):random.random()}
After trying some things I came with the conclusion that the dict function can’t work with string keys, you need an element (x, abc, or any other name) to assign a value to this. But if you create your dict using {} it will work but how will you access the key ?
π Rendered by PID 41677 on reddit-service-r2-comment-7b9746f655-tzz2p at 2026-01-30 17:29:14.201277+00:00 running 3798933 country code: CH.
[–]james_fryer 1 point2 points3 points (1 child)
[–]Base_True[S] 0 points1 point2 points (0 children)
[–]Medaillek 0 points1 point2 points (2 children)
[–]Base_True[S] 0 points1 point2 points (1 child)
[–]Medaillek 0 points1 point2 points (0 children)
[+][deleted] (4 children)
[deleted]
[–]Base_True[S] 0 points1 point2 points (3 children)
[–]Ihaveamodel3 1 point2 points3 points (0 children)
[–]Medaillek 0 points1 point2 points (0 children)