0

I have recently begun using Python after having primarily used C and PHP for a long time. One difference that I have noticed is how Python treats a Dictionary and a List as different things where Indexed and Associative arrays are only kind of different.

I am assuming that PHP and C act this way because it is simple. (Although please correct me if I am wrong.) Is there a source that talks about the reasons why Python (and other languages) have chosen to separate these data structures to such an extent?

I am not asking for, or implying, any kind of a ranking about what is better, just trying to gain understanding of something new to me.

Thanks

(In response to one of the comments, I apparently incorrectly remembered using Associative arrays in C.)

  • Here is the discussion on python's different data structures that you may find useful https://stackoverflow.com/questions/3489071/in-python-when-to-use-a-dictionary-list-or-set – Vivek Harikrishnan Nov 27 '17 at 14:30
  • 1
    I would think PHP would be the odd one out here. What C implementation of an associative array are you using? The two are fundamentally different data structures. Dictionaries in Python are hashtables. – ryachza Nov 27 '17 at 14:33

1 Answers1

1

I think PHP might be causing some confusion here. My understanding is that all arrays in PHP are associative, but they maintain the order of the added keys for iteration and default to using sequential integer keys giving the illusion of being the same as arrays in other environments.

Dictionary is a synonym for associative array, and in Python the particular implementation used is a hashtable. Other environments may use different or offer multiple implementations.

Lists in Python are like vectors in C++ - arrays underneath but handle dynamic resizing. The term "list" is ambiguous in general, though.

I'm not aware of any built-in associative array in C.

These links might be helpful for further reading:

https://en.wikipedia.org/wiki/Associative_array https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(associative_array)

ryachza
  • 4,460
  • 18
  • 28
  • So it essentially comes down to C only has Indexed arrays (without adding extras), PHP only has associative arrays, but Python has both. – user3034958 Nov 28 '17 at 14:48
  • @user3034958 I'll say yes, but that digging into the details of the different implementations would still be valuable. PHP's implementation is very unique in terms of doing a lot of magic. C's arrays are fixed size, whereas Python's lists are variable. Python's dictionaries don't maintain the order the keys are added, but you could use `OrderedDict` from `collections` if you need it. PHP and Python in general are "batteries included", whereas C expects you to provide your own implementations of many common data structures. – ryachza Nov 28 '17 at 15:08
  • @user3034958 Perhaps including the C++ STL (specifically `vector` and `map`) in the comparison would help. `map` is an associative container, but the iteration order is the order of the keys (different from both PHP (order added) and Python (arbitrary)). `vector` is pretty much the same as a Python list. – ryachza Nov 28 '17 at 15:17