PHP’s Array vs Python’s List

To begin with, I have never ever written a useful program that didn’t involve some kind of dynamic in-memory storage. Luckily, I do most of the work with high-level programming languages, so data structures like doubly linked list are long gone. 🙂

I will show you differences and similarities of PHP’s array and Python’s list for cases that appear everyday in my life. Prepare for reading some code and let’s go!

How to create a list and access it’s elements

People, who do PHP, definitely know what an array is. Take a look:

$items = ['hello', 'I', 'am', 'PHP', 'array'];
echo $items[0]; // prints hello
echo $items[3]; // prints PHP

First of all, this piece of code shows how to create an array and store it in variable. Moreover, it shows how to access elements of an array. And indexing begins with 0. Now, lets look how it looks like in Python:

items = ['hello', 'I', 'am', 'Python', 'list']
print items[0] # prints hello
print items[3] # prints Python

You can see that it is almost the same! Only syntactic sugar. 🙂

Adding element to the end

Most often, you will have to append some value at the end of your data structure. If you are reading file line by line, you append line that you had recently read and go on till the end of file. This example is one of many cases that this operation is used.

Let’s take a look how previous example can be rewritten:

$items = ['hello', 'I'];
$items[] = 'am';
$items[] = 'PHP';
$items[] = 'array';
echo $items[0]; // prints hello
echo $items[3]; // prints PHP

As you can see, in order to add element to the end of array in PHP, you have to put a pair of brackets without any index.

In Python, same result can be achieved this way:

items = ['hello', 'I']
items.append('am')
items.append('python')
items.append('list')
print items[0] # prints hello
print items[3] # prints python

This time, in python you have to call method append() to add element to the end of list. It requires writing more code, but, in my opinion, it is much more readable and understandable than PHP.

Finding element

I often find myself needing an index of specific element in array. In PHP, a function called array_search() fits this purpose. Check the code:

$items = ['hello', 'I', 'am', 'PHP', 'array'];
echo array_search('hello', $items); // prints 0
echo array_search('PHP', $items); // prints 3

Even after long years with PHP, I still had to visit documentation and see how this array_search() works. More often than not, I’d swap arguments and give array as first. I didn’t really groan about that – it was the fastest way to find it out.

But check out how it is done in Python:

items = ['hello', 'I', 'am', 'Python', 'list']
print items.index('hello') # prints 0
print items.index('Python') # prints 3

If that is not neat, then I don’t know what is. You just call index() method on a list and it finds index of that element. The code speaks for itself. I mean, I will never forget how to find an index of element in list. 🙂

Removing element

The last common operation is removing element from an array. Let’s say you are doing some filter and want to drop off some values that are less than 5. So you loop and perform unset() as many times as needed.

$items = ['hello', 'I', 'am', 'PHP', 'array'];
echo $items[3]; // prints PHP
unset($items[3]);
echo $items[3]; // Notice:  Undefined offset: 3 in [...] on line 4

Notice that indexing of array doesn’t change after removing element from array. Check same example written in Python:

items = ['hello', 'I', 'am', 'Python', 'list']
print items[3] # prints Python
del items[3]
print items[3] # prints list

It doesn’t differ that much. First of all, we use del keyword to delete element. Second, indexing is changed. So you don’t have any cracks in your list, it remains discretely continuous (all indexes are always in order).

Conclusion

If you are like me, then you will get a good sense about Python’s list very quickly. My experience with it was great! I got it on first few tries and found it to be very well designed.

What was your experience with Python’s lists? Maybe it was harder than this? Let me know in the comments! 🙂

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.