A story about a person who uses Python addicted to the judgment of an empty JavaScript dictionary

I usually use Python for machine learning programming, but I also need to do around the screen, and at that time, is it the code that judges the JavaScript dictionary with the if statement? I thought. Until then, it's a misunderstanding.

For Python

I wrote the following if statement to determine if a dictionary is empty in Python. If x is an empty dictionary or null, it will be False.

sample.py


x = {}
if x:
  print("x is not empty");
else:
  print("x is empty");

Execution result

x is empty

For JavaScript

On the other hand, in JavaScript, it is True in the case of an empty dictionary.

sample.js


var x = {};
if (x) {
  console.log("x is not empty");
} else {
  console.log("x is empty");
}
x is not empty

There seem to be various ways to determine if it is empty, but it seems that it can be done by checking the number of keys with Object.keys (x).

sample.js


var x = {};
if (Object.keys(x).length) {
  console.log("x is not empty");
} else {
  console.log("x is empty");
}

I inadvertently thought and coded it in the same way as Python, and I was worried that it wouldn't work ...

References

It is easy to understand whether the judgment result of JavaScript if (x) is True/False for each object. -[JavaScript] Judgment of null, undefined, 0, empty string (”), false, etc.

Recommended Posts

A story about a person who uses Python addicted to the judgment of an empty JavaScript dictionary
A story about trying to introduce Linter in the middle of a Python (Flask) project
A story that struggled to handle the Python package of PocketSphinx
How to check the memory size of a dictionary in Python
The story of making a tool to load an image with Python ⇒ save it as another name
A note about the python version of python virtualenv
An article summarizing the pitfalls addicted to python
A story made by a person who has no knowledge of Python or Json
A story about an engineer who came only on the server side created a portfolio
How to write a list / dictionary type of Python3
A story about a Python beginner trying to get Google search results using the API
A story about changing the master name of BlueZ
A magic word (?) That may save people who are addicted to building using the Intel compiler of Python + Numpy.
The story of automatic language conversion of TypeScript / JavaScript / Python
Python Note: The mystery of assigning a variable to a variable
Don't take an instance of a Python exception class directly as an argument to the exception class!
Get the value of a specific key up to the specified index in the dictionary list in Python
A story about a Python beginner who was about to be crushed by ModuleNotFoundError: No module named'tweepy'
Create a 2D array by adding a row to the end of an empty array with numpy
A story about porting the code of "Try and understand how Linux works" to Rust
A person who wants to clear the D problem with ABC of AtCoder tried to scratch
How to convert an array to a dictionary with Python [Application]
A story about how to specify a relative path in python.
The story of introducing jedi (python auto-completion package) to emacs
A story about how to deal with the CORS problem
A story about an amateur making a breakout with python (kivy) ①
A story about trying to implement a private variable in Python.
[Fixed] I was addicted to alphanumeric judgment of Python strings
About the ease of Python
About the features of Python
I wrote AWS Lambda, and I was a little addicted to the default value of Python arguments
The story that the version of python 3.7.7 was not adapted to Heroku
How to know the internal structure of an object in Python
A story about adding a REST API to a daemon made with Python
How to check the memory size of a variable in Python
The story of making a standard driver for db with python.
A story about trying to run multiple python versions (Mac edition)
[Pythonista] The story of making an action to copy selected text
[python] A note that started to understand the behavior of matplotlib.pyplot
The story of making a module that skips mail with python
[Python] A simple function to find the center coordinates of a circle
[Python] A program that rotates the contents of the list to the left
Create a compatibility judgment program with the random module of python.
The story of Python and the story of NaN
[Fabric] I was addicted to using boolean as an argument, so make a note of the countermeasures.
A story addicted to Azure Pipelines
A super beginner who does not know the basics of Python tried to graph the stock price of GAFA
A story about someone who wanted to import django from a python interactive shell and save things to a DB
[AtCoder for beginners] A story about the amount of calculation that you want to know very roughly
A story about trying to improve the testing process of a system written in C language for 20 years
About the basics list of Python basics
The story of writing a program
A story about creating a program that will increase the number of Instagram followers from 0 to 700 in a week
The story of making a university 100 yen breakfast LINE bot with Python
[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement
[Python] A program that calculates the number of socks to be paired
A story I was addicted to when inserting from Python to a PostgreSQL table
A story about my new study of Python after 3 years of MATLAB experience
Python Note: When you want to know the attributes of an object
[Introduction to Python] How to sort the contents of a list efficiently with list sort
A story of a person who started aiming for data scientist from a beginner