I\'m new to python and I was reading about Dictionaries. And from my previous experience with langages like javascript they seemed like objects to me. Dictionaries can store
Consider an identifier used in an initializer, such as
b = {i:j}
In Python both i and j are evaluated but in Javascript, only j is evaluated. In Javascript you can also write the dot notation, using the identifier i. Hence in Python,
i='k'
j=1
b = {i:j}
b['k'] # -> 1
In Javascript,
i='k'
j=1
b = {i:j}
b['i'] // -> 1
b.i // -> 1
// b[i], b['k'] and b.k are not defined
In Javascript, using the identifier in dot notation is completely identical in all cases to using a string that "looks like" the identifier in array notation. Hence, b = { 'i':1 } ; b['i'] // -> 1 b.i // -> 1 When a number or boolean is used in a dictionary, Javascript will access the element using a string representation of the number or boolean. Not so in Python — a string and a number (or boolean) are different hashables.
However,
//b[i] will be valid in this case of javascrip code
b = {}
var i = 'k';
b[i] = 2
console.log(b); // -> {'k':2}
//thus b[i] = b['k'] = 2
So, even though there are differences but all the applications of dictionary in python can be performed by the object in javascript and vice versa.
From :
In Python, dictionaries are a form of mapping type. They can be initialized using a sequence of comma-separated name: value pairs, enclosed in curly braces. They are accessed using array notation involving square braces. The key can be any hashable, including numbers and strings.
In Javascript, a dictionary is the same as an object. It can be initialized using the same syntax as Python. The key can be a number, a string, or an identifier. Because the dictionary is also an object, the elements can be accessed either using array notation, e.g. b[i], or using property notation, e.g. b.i.
Consider an identifier used in an initializer, such as
b = {i:j}
In Python both i and j are evaluated, but in Javascript, only j is evaluated. In Javascript you also have the privilege of writing in the dot notation, using the identifier i. Hence in Python,
i='k' j=1 b = {i:j} b['k'] # -> 1
In Javascript,
i='k' j=1 b = {i:j} b['i'] // -> 1 b.i // -> 1 // b[i], b['k'] and b.k are not defined
In Javascript, using the identifier in dot notation is completely identical in all cases to using a string that "looks like" the identifier in array notation. Hence, b = { 'i':1 } ; b['i'] // -> 1 b.i // -> 1 When a number or boolean is used in a dictionary, Javascript will access the element using a string representation of the number or boolean. Not so in Python — a string and a number (or boolean) are different hashables.
If you are interested in differences between both languages, then look at ans
Keys in Python dictionaries must be hashable (e.g. a string, a number, a float), while JavaScript does not have such a requirement. Consider this example:
The following is a valid object in JavaScript:
const javascriptObject = { name: 'Alexander Pushkin', year: 1799 }
However, it would be invalid as a Python dictionary:
python_dictionary = {name: 'Alexander Pushkin', year: 1799}
# Results in a NameError: name 'name' is not defined
A quick fix would be to convert the Python dictionary's keys into strings:
my_dictionary = {'name': 'Alexander Pushkin', 'year': 1799}