Categories
PHP Python

Creating Dictonaries from Arrays / Lists

PHP:

In PHP, we can create a new associative array or dictionary by using the array_combine() function. Just feed two arrays as the parameters to this function and you get back a dictionary. The first array elements are converted into keys and the second array elements are used as values.

Python:

For Python, we first have to zip() two lists into another lists of tuples containing one element from both lists. Then we use the dict() call on this newly created list to create a dictionary.

I elaborated the process in the above example for beginners. But you’d often see advanced python programmers do it like this:

Categories
Python

Creating Conditional Lists in Python

This post is meant to be short and self explanatory. We can easily create lists in python based on given conditions.

Have a look:

It would create a list containing 1 to 99. I get impressed by the power of Python the more I dig deeper into this fantastic language.

Categories
PHP

Converting Array to Object in PHP

Here’s a funny little code snippet that demonstrates the flexibility of PHP. Here, we first define an array. Then cast it into an object. And guess what, the keys of the arrays become public variables of the newly created object.

I was impressed to find it out…. Just have a look :

Cool, eh ? 😉