dict slice assignment with tuples?

Fredrik Lundh fredrik at pythonware.com
Wed Sep 15 19:20:21 EDT 1999


Nathan Clegg <nathan at islanddata.com> wrote:
> I appreciate python's ability to assign several values to several keys in
> a dictionary, such as in:
> 
> a = {}
> a['b', 'c', 'd'] = [4, 5, 6]
> 
> I would like to be able to do this with a single tuple or list instead. 
> So, to accomplish the above, I would like to be able to do something like:
> 
> keys = ['b', 'c', 'd']
> a = {}
> a[keys] = [4, 5, 6]

how about:

    keys = 'b', 'c', 'd'
    a = {}
    a[keys] = [4, 5, 6]

or perhaps:

    keys = ['b', 'c', 'd']
    a = {}
    a[tuple(keys)] = [4, 5, 6]

(note that a['b', 'c', 'd'] = [4, 5, 6] is the
same thing as a[('b', 'c', 'd')] = [4, 5, 6])

</F>





More information about the Python-list mailing list