Wednesday, July 21, 2010

Wonderful Lists and Dictionaries

I used to program in C, C++ and Java and I remember that you always had to go an extra effort to create hash arrays! Boy were those the days and a major pain in the arse.

Now what I really love about Python is the lists and even more the dictionary data types! Man these data types are what those other languages should have by default and not monkey fuss around just getting the array to expand properly and not being concerned about constraints of a array or linked list.

compare for example:

in C to just use an array it required the data type and then a constant to initialize to structure

Ex 1:

int my_array[10]

for(int i = 0; i < 10; i++)

{

my_array[i] = i

}

in Python you just use

my_array=range(0,10)

Now to add more than 10 items to this array that required either a pointer switch or a for loop to copy the current items to the new array. Pain in the butt! Though in C++ you can create linked lists to allow this to work better

int my_new_array[11]

for(int i =0; i < 10; i++)

{

my_new_array[i] = my_array[i]

}

In python it is simply:

my_array.append(new_number)

That's just a start of the beauty of Python over C, I just enjoy the elegance of python.

No comments:

Post a Comment