Python Memo 2: Dictionary vs. Set

Basis of Dictionaries and Sets

A dictionary is composed of a series of key-value mapping elements. In Python 3.7 +, a dictionary is determined to be ordered, however, before 3.6, a dictionary was disordered. Its length is mutable and elements can be deleted and changed arbitrarily. Compared with lists and tuples, the performance of dictionaries is better, especially for search, add, and delete operations. A dictionary can be completed within a constant time complexity. A set and a dictionary are basically the same, the only difference is that a set has no key-value pairing and is a series of disordered and unique element combinations. 

First, let's look at the creation of dictionaries and collections. There are usually the following ways;

Python Memo 1: Tuple vs. List

Tuple vs. List

Both Tuple and List can store mixed types of values.

Plain Text
 




x


 
1
a = [1,"Hello",bool(0)]
2
b = (1,"Hello",bool(0))
3
a, b
4
Out: ([1, 'Hello', False], (1, 'Hello', False))


Tuple is immutable and length is fixed. List is mutable and length is not fixed.

Load and Compile Dynamical HTML in AngularJS

As a project requirement, we have to load HTML content with CSS (in JSON response) from the backend dynamically and compile it in an Angular application. We also need to support those Angular directives defined in HTML. This article will describe the solution for both AngularJS and Angular.

AngularJS

To compile dynamical HTML in AngularJS, we just need to simply inject $compile and use it to compile the HTML after injecting. So here we use a JQuery function to fetch HTML elements and set HTML into it.