Comparison of programming languages (associative array): Difference between revisions

Content deleted Content added
Prolog: copy editing
Python: copy editing, linking, etc.
Line 1,493:
Some versions of [[Prolog]] include dictionary ("dict") utilities.<ref>[http://www.swi-prolog.org/pldoc/man?section=dicts "Dicts: structures with named arguments"]</ref>
 
=== [[Python (programming language)|Python]] ===
In [[Python]], associative arrays are called ''"[[Python syntax and semantics#Collection types|dictionaries]]''". Dictionary literals are markeddelimited withby curly braces:
 
<syntaxhighlight lang=Python>
Line 1,504:
</syntaxhighlight>
 
To access an entry in Python simply use the array indexing operator. For example,:
<syntaxhighlight lang=Pycon>
>>> phonebook['Sally Smart']
'555-9999'
</syntaxhighlight>
 
An example loopLoop [[iterator#Python|iterating]] through all the keys of the dictionary:
 
<syntaxhighlight lang=Pycon>
Line 1,529 ⟶ 1,530:
</syntaxhighlight>
 
Dictionary keys can be individually deleted using the <code>del</code> statement. The corresponding value can be returned before the key-value pair areis deleted using the "pop" method of "dict" typestype:
 
<syntaxhighlight lang=Pycon>
Line 1,538 ⟶ 1,539:
</syntaxhighlight>
 
Python 2.7 and 3.x also supportssupport dictionary comprehensions[[list comprehension]], a compact syntax for generating a dictionary from any iterator:
 
<syntaxhighlight lang=Pycon>
>>> square_dict = {i: i*i for i in range(5)}
Line 1,547 ⟶ 1,549:
</syntaxhighlight>
 
Strictly speaking, a dictionary is a super-set of an associative array, since neither the keys or values are limited to a single datatype. One could think of a dictionary as an ''"associative list''" using the nomenclature of Python. For example, the following is also legitimate:
 
<syntaxhighlight lang=Python>