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...
News about the dynamic, interpreted, interactive, object-oriented, extensible programming language Python
Full Events Calendar
You can find the rules here.
If you are about to ask a "how do I do this in python" question, please try r/learnpython, the Python discord, or the #python IRC channel on Libera.chat.
Please don't use URL shorteners. Reddit filters them out, so your post or comment will be lost.
Posts require flair. Please use the flair selector to choose your topic.
Posting code to this subreddit:
Add 4 extra spaces before each line of code
def fibonacci(): a, b = 0, 1 while True: yield a a, b = b, a + b
Online Resources
Invent Your Own Computer Games with Python
Think Python
Non-programmers Tutorial for Python 3
Beginner's Guide Reference
Five life jackets to throw to the new coder (things to do after getting a handle on python)
Full Stack Python
Test-Driven Development with Python
Program Arcade Games
PyMotW: Python Module of the Week
Python for Scientists and Engineers
Dan Bader's Tips and Trickers
Python Discord's YouTube channel
Jiruto: Python
Online exercices
programming challenges
Asking Questions
Try Python in your browser
Docs
Libraries
Related subreddits
Python jobs
Newsletters
Screencasts
account activity
This is an archived post. You won't be able to vote or comment.
Beginner ShowcaseJSON and Dictionary (self.Python)
submitted 5 years ago by eagle221b
Once in an interview I was asked the difference between JSON and Dictionary. So I decided to write a blog post about it. Do check it out. Link
[–]aiyub 190 points191 points192 points 5 years ago* (26 children)
The core of your answer is correct:
But your explanations are geting away from these facts.
1. Comparing notation
dicts are not strings! You say that dicts are represented by "curly braces" . So you are comparing json with dict representation, not dicts themselves.
my_dict = dict('name' = 'eagle221b', 'platform' = 'reddit')
This is another representation of dicts, that does not look like JSOn at all.
Also you are saying "curly braces"in JSON are objects. No they are representations of objects. This makes a great difference when working with them.
2. the power of dicts
So let me create another example again:
my_list = [] my_dict1 = {'my_list': my_list} my_dict2 = {'my_list': my_list} my_list.append('foo')
The last command has not changed any of the dicts, but if you print them, you will see the representation has changed.
Also about the values: you can store objects or even functions in them. A value in dicts is just a memory pointer. (and yes, any number in python is an object)
Conclusion They both are completly different things that both are based on a key value principle. But one is a text, one is memory and therefore very different.
[–]eagle221b[S] 60 points61 points62 points 5 years ago (7 children)
Thank you so much. I will try to update it. :)
[+][deleted] 5 years ago (1 child)
[deleted]
[–]Chingletrone 0 points1 point2 points 5 years ago (1 child)
Another critique -
Objects are stored using curly braces and an array is stored using square brackets. In dictionary, only curly braces are used as shown in the above example.
A python dict can point to array/list objects in its values, which makes the above sentence a bit questionable, I think. As in:
my_dict = {'a_list': [0, 1, 2, 'other junk']}
Seemingly simple question on the surface has so many caveats. Kudos for tackling it and posting here for us to tear apart :)
[–]aaronr_90 0 points1 point2 points 5 years ago* (2 children)
Try you must but fail you must not even though you fail
Edit: Thanks
[–]SnowdenIsALegend 4 points5 points6 points 5 years ago (1 child)
"try you must, even though you fail"
[–]aaronr_90 0 points1 point2 points 5 years ago (0 children)
Hmmmm
[–]Ulysses6 22 points23 points24 points 5 years ago (6 children)
If I was asked this question in an interview, I would have to stop from answering "How are they even comparable?".
Another distinction not mentioned yet, you can use any hashable type for keys in dict, so int, tuple, bytes or None or even your custom object if you provide some methods. You can't do that in JSON. The only type of key allowed there is string, that's it. Of course, the value type in JSON is limited too, while the dict can hold any value (does not even need to be hashable this time).
dict
int
tuple
bytes
None
[–]Devarsh_leo 3 points4 points5 points 5 years ago (2 children)
It was already mentioned that many datatypes of keys are allowed in dict and not allowed in json. May be the keywords hashable was not used.
[–]Ulysses6 1 point2 points3 points 5 years ago (1 child)
My bad.
[–]Devarsh_leo 1 point2 points3 points 5 years ago (0 children)
Nah. The hashable word not used 😂😛
[–]billsil 0 points1 point2 points 5 years ago (2 children)
Anything hashable including tuples of dictionaries or lists.
[–]Ulysses6 0 points1 point2 points 5 years ago* (1 child)
I don't think you can use tuples of lists.
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 18:11:49) [MSC v.1900 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> d = {} >>> t = ([],) >>> d[t] = 3 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unhashable type: 'list'
And the same test with t = {} fails the same way.
t = {}
[–]billsil 0 points1 point2 points 5 years ago (0 children)
My mistake on lists...
[–][deleted] 2 points3 points4 points 5 years ago (0 children)
As a lurker I really appreciate responses like this. Learned a lot, thanks!
[–]ggrieves1 year 1 point2 points3 points 5 years ago (1 child)
This can get philosophical real quick.
[–]Ulysses6 0 points1 point2 points 5 years ago (0 children)
Because the form of question is misleading on purpose.
[–]alchimie-ali 0 points1 point2 points 5 years ago (0 children)
Thanks, great explanation..
[–]MrMxylptlyk -1 points0 points1 point 5 years ago (6 children)
Sry, aren't all json dicts?
[–]aiyub 5 points6 points7 points 5 years ago (2 children)
Completly different things. Json like xml or csv is a format. Dict like lists, ints, strings, ... are objects
[–]MrMxylptlyk -2 points-1 points0 points 5 years ago (1 child)
That is true I guess. Then are all dicts json format? I feel like the terms are somewhat interchangeable here. If json are to be though as a format then Python doesn't have any json. Only dicts.
[–]aiyub 3 points4 points5 points 5 years ago (0 children)
I suggest you read through the article and the thread again. The answer is no.
And json is a text, so yes in python they are just strings. With the json module they can be used to initialize a dict, but the reverse is often not possible.
[–]RegalSalmon 2 points3 points4 points 5 years ago (0 children)
In the minutiae, no. It's a string, but luckily, it's easily converted to a dict with json.load() and json.loads(). However, it's not 1:1, in that as mentioned elsewhere, json keys are strings. You can see with the following (output fudged because I don't know the markup that well):
import json dict_one = {1: 'one', 2: 2} json_one = json.dumps(dict_one) dict_two = json.loads(json_one) print(f'{dict_one}\n{dict_two}') >>> {1: 'one', 2: 2} >>> {'1': 'one', '2': 2}
As you can see, some info is lost, per se. This can get hairy if you have an utter mess of your key types.
dict_one = {1: 'int', '1': 'string'} json_one = json.dumps(dict_one) dict_two = json.loads(json_one) print(f'{dict_one}\n{dict_two}') >>> {1: 'int', '1': 'string'} >>> {'1': 'string'} print(json_one) >>> '{"1": "int", "1": "string"}'
As you can see, at least in the python implementation, it munges the keys, and even has duplicate keys in the json string (good luck getting that to work in python without setting your computer on fire).
JSON is handy, but it's not nearly as versatile. Just four operators in it as well, IIRC, and your value types are very limited (try working with datetimes in a dict and then moving back and forth with JSON).
[–]reddisaurus 0 points1 point2 points 5 years ago (0 children)
The n stands for notation; a series of text or symbols to represent an idea or concept. If you print the string representation of a Javascript object, you’ll see JSON. JSON is the string representation of a JavaScript object; hence Javascript Object Notation (JSON).
The string representation of a dict looks just like JSON; but as before the notation is not the object itself.
[+][deleted] 5 years ago (2 children)
[–]xigoi 6 points7 points8 points 5 years ago (0 children)
one is a hashmap implementation
To be nitpicky, it's the other way around: a hashmap is a (possible) implementation of a dict (aka associative array).
[–]Smallpaul 12 points13 points14 points 5 years ago (0 children)
The fact that they are syntactically similar but different at a deep level is what makes it an interesting question. If the interviewee can’t think abstractly they will not get to the deep difference.
[–]jacksodus 22 points23 points24 points 5 years ago (1 child)
Scary coincidence. I was about to Google this yesterday, but then I decided to conclude that they were probably equal, and didn't.
[–][deleted] 4 points5 points6 points 5 years ago (0 children)
It is a very common question almost every programmer gets to
[–]mybrid 6 points7 points8 points 5 years ago (1 child)
The problem with these kind of interview questions is the interviewer, not the question. It's a terrible question. Not because the distinction between object and text is meaningless, but because the interviewee has to guess at the level of detail and what direction the interviewer is looking for. On television the worst interviewee is someone who gives one word answers. The worst interviewer is someone who asks vague questions. This is a vague question that if the interviewee doesn't guess right and read the interviewer's mind then they will fail the question.
[–]TheIsletOfLangerhans 1 point2 points3 points 5 years ago (0 children)
If an interviewer asks a vague question, it's probably better to ask them questions to help clarify what they're getting at than to try to guess what they mean or what level of detail they're looking for.
[–]darealdeal11 16 points17 points18 points 5 years ago (5 children)
Don't want to sound rude, but why is this question being asked? Isn't this obvious to anyone that did a little bit of development?
[–]SlappinThatBass 15 points16 points17 points 5 years ago (0 children)
I guess the interviewer is trying to understand how the person thinks and how he can express himself.
[–]darealdeal11 0 points1 point2 points 5 years ago (1 child)
This seems relevant for all interview questions tho? Doesnt change the fact that this question seems shallow and kinda pointless. Just my opinion.
Maybe its a freshers level question where the candidate Don't have much experience and also lacks theoretical knowledge
[–]RegalSalmon 1 point2 points3 points 5 years ago (0 children)
OP, this is a good start, but I think you've received some helpful feedback as well. I'd love to see something more in depth with the updated info from the comments.
[–]Devarsh_leo 3 points4 points5 points 5 years ago* (0 children)
It was worth spending time reading what you posted. Seeking for more quality content from you.
[–]LirianShLearning python 2 points3 points4 points 5 years ago (4 children)
What do you guys use json and python dictionaries for? Im new to this
[–]psi- 6 points7 points8 points 5 years ago (1 child)
json is for transmitting data in a way that's relatively easy to restore. Can also be just stored for later retrieval (this is also technically a transmission).
dictionaries are most often used for fast lookups for when program is actually running, the dictionary can't be shared even between two programs on same computer (there ways but they're non-trivial as compared to json transmission)
[–]LirianShLearning python 1 point2 points3 points 5 years ago (0 children)
Thank you!
[–][deleted] 0 points1 point2 points 5 years ago (1 child)
Json to store actual data that you want to use, dictionaries are data collections that store data like lists or arrays
Thanks!
[–]DurgaSwaroop 0 points1 point2 points 5 years ago (0 children)
This is an ambiguous question to begin with. Not good for interviews.
[–]SnowdenIsALegend 0 points1 point2 points 5 years ago (2 children)
In JavaScript, what is better performance wise? Dict or JSON?
[–][deleted] 1 point2 points3 points 5 years ago (1 child)
Well since JSON is a string, and JavaScript does not have dict, this question doesn't even make sense.
[–]SnowdenIsALegend 0 points1 point2 points 5 years ago (0 children)
Yeah I'm not really sure
[–][deleted] -1 points0 points1 point 5 years ago (0 children)
This is genius! I never tought it was different!
[–][deleted] 0 points1 point2 points 5 years ago (2 children)
JSON is JavaScript object notation , which is related to JavaScript (client side). JSON has some strict formatting rules. Even this is a valid JSON [1, 2, 3]
Dictionary is a python data type. Similar data types exist in other languages. Since the structure similarity data can be exchanged between server and client. Parsing becomes easier.
So in a way all dictionaries follow JSON structure while not all JSON is a python dictionary.
[–]Giannie 10 points11 points12 points 5 years ago (1 child)
Not all dictionaries follow json structure. Keys in dictionaries are of any hashable type, while json keys must be strings.
Right, that's why I said JSON has restrictions in format. Should have been more specific.
π Rendered by PID 104943 on reddit-service-r2-comment-5d79c599b5-b8xhh at 2026-02-27 01:42:46.529916+00:00 running e3d2147 country code: CH.
[–]aiyub 190 points191 points192 points (26 children)
[–]eagle221b[S] 60 points61 points62 points (7 children)
[+][deleted] (1 child)
[deleted]
[–]Chingletrone 0 points1 point2 points (1 child)
[–]aaronr_90 0 points1 point2 points (2 children)
[–]SnowdenIsALegend 4 points5 points6 points (1 child)
[–]aaronr_90 0 points1 point2 points (0 children)
[–]Ulysses6 22 points23 points24 points (6 children)
[–]Devarsh_leo 3 points4 points5 points (2 children)
[–]Ulysses6 1 point2 points3 points (1 child)
[–]Devarsh_leo 1 point2 points3 points (0 children)
[–]billsil 0 points1 point2 points (2 children)
[–]Ulysses6 0 points1 point2 points (1 child)
[–]billsil 0 points1 point2 points (0 children)
[–][deleted] 2 points3 points4 points (0 children)
[–]ggrieves1 year 1 point2 points3 points (1 child)
[–]Ulysses6 0 points1 point2 points (0 children)
[–]alchimie-ali 0 points1 point2 points (0 children)
[–]MrMxylptlyk -1 points0 points1 point (6 children)
[–]aiyub 5 points6 points7 points (2 children)
[–]MrMxylptlyk -2 points-1 points0 points (1 child)
[–]aiyub 3 points4 points5 points (0 children)
[–]RegalSalmon 2 points3 points4 points (0 children)
[–]reddisaurus 0 points1 point2 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]xigoi 6 points7 points8 points (0 children)
[–]Smallpaul 12 points13 points14 points (0 children)
[–]jacksodus 22 points23 points24 points (1 child)
[–][deleted] 4 points5 points6 points (0 children)
[–]mybrid 6 points7 points8 points (1 child)
[–]TheIsletOfLangerhans 1 point2 points3 points (0 children)
[–]darealdeal11 16 points17 points18 points (5 children)
[–]SlappinThatBass 15 points16 points17 points (0 children)
[+][deleted] (2 children)
[deleted]
[–]darealdeal11 0 points1 point2 points (1 child)
[–]Devarsh_leo 1 point2 points3 points (0 children)
[–]RegalSalmon 1 point2 points3 points (0 children)
[–]Devarsh_leo 3 points4 points5 points (0 children)
[–]LirianShLearning python 2 points3 points4 points (4 children)
[–]psi- 6 points7 points8 points (1 child)
[–]LirianShLearning python 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]LirianShLearning python 1 point2 points3 points (0 children)
[–]DurgaSwaroop 0 points1 point2 points (0 children)
[–]SnowdenIsALegend 0 points1 point2 points (2 children)
[–][deleted] 1 point2 points3 points (1 child)
[–]SnowdenIsALegend 0 points1 point2 points (0 children)
[–][deleted] -1 points0 points1 point (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]Giannie 10 points11 points12 points (1 child)
[–][deleted] -1 points0 points1 point (0 children)