Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

Is there something similar to list comprehension in dict?

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


pengyu.ut at gmail

Nov 19, 2009, 7:18 PM

Post #1 of 14 (497 views)
Permalink
Is there something similar to list comprehension in dict?

I'm wondering if there is something similar to list comprehension for
dict (please see the example code below).


d = dict(one=1, two=2)
print d

def fun(d):#Is there a way similar to list comprehension to change the
argument d so that d is changed?
d=dict(three=3)

fun(d)
print d

def fun1(d):
d['one']=-1

fun1(d)
print d


L = [1, 2]
print L

def fun2(L):#this doesn't have any effect on the argument L
L=[]

fun2(L)
print L#[1, 2]

def fun3(L):# argument L is changed
L[:]=[1, 2, 3]

fun3(L)
print L#[1, 2, 3]
--
http://mail.python.org/mailman/listinfo/python-list


michele.simionato at gmail

Nov 20, 2009, 12:19 AM

Post #2 of 14 (481 views)
Permalink
Re: Is there something similar to list comprehension in dict? [In reply to]

On Nov 20, 4:18 am, Peng Yu <pengyu...@gmail.com> wrote:
> I'm wondering if there is something similar to list comprehension for
> dict

Yes, but only in Python 3:

>>> {(i, x) for i, x in enumerate('abc')}
{(0, 'a'), (1, 'b'), (2, 'c')}
--
http://mail.python.org/mailman/listinfo/python-list


stefan_ml at behnel

Nov 20, 2009, 12:24 AM

Post #3 of 14 (474 views)
Permalink
Re: Is there something similar to list comprehension in dict? [In reply to]

Peng Yu, 20.11.2009 04:18:
> I'm wondering if there is something similar to list comprehension for
> dict (please see the example code below).

A list comprehension is an expression that produces a list, e.g.

[ i**2 for i in range(10) ]

Your example below uses a slice assignment.


> def fun(d):#Is there a way similar to list comprehension to change the
> argument d so that d is changed?
> d=dict(three=3)
> [...]
> def fun3(L):# argument L is changed
> L[:]=[1, 2, 3]

You can use d.update(...)

It accepts both another dict as well as a generator expression that
produces item tuples, e.g.

d.update( (i, i**2) for i in range(10) )

Does that help?

Stefan
--
http://mail.python.org/mailman/listinfo/python-list


stefan_ml at behnel

Nov 20, 2009, 12:26 AM

Post #4 of 14 (480 views)
Permalink
Re: Is there something similar to list comprehension in dict? [In reply to]

Stefan Behnel, 20.11.2009 09:24:
> You can use d.update(...)
>
> It accepts both another dict as well as a generator expression that
> produces item tuples, e.g.
>
> d.update( (i, i**2) for i in range(10) )

This also works, BTW:

>>> d = {}
>>> d.update(value=5)
>>> d
{'value': 5}

Stefan
--
http://mail.python.org/mailman/listinfo/python-list


patrick.just4fun at gmail

Nov 20, 2009, 1:21 AM

Post #5 of 14 (477 views)
Permalink
Re: Is there something similar to list comprehension in dict? [In reply to]

Peng Yu wrote:
> I'm wondering if there is something similar to list comprehension for
> dict (please see the example code below).

Do you mean something like this:

>>> {i:i+1 for i in [1,2,3,4]}
{1: 2, 2: 3, 3: 4, 4: 5}

This works in python3, but not in python2

- Patrick
--
http://mail.python.org/mailman/listinfo/python-list


paul.nospam at rudin

Nov 20, 2009, 1:29 AM

Post #6 of 14 (478 views)
Permalink
Re: Is there something similar to list comprehension in dict? [In reply to]

Patrick Sabin <patrick.just4fun [at] gmail> writes:

> Peng Yu wrote:
>> I'm wondering if there is something similar to list comprehension for
>> dict (please see the example code below).
>
> Do you mean something like this:
>
>>>> {i:i+1 for i in [1,2,3,4]}
> {1: 2, 2: 3, 3: 4, 4: 5}
>
> This works in python3, but not in python2

Of course in python 2 you can do:

>>> dict((i, i+1) for i in [1,2,3,4])
{1: 2, 2: 3, 3: 4, 4: 5}
--
http://mail.python.org/mailman/listinfo/python-list


tjreedy at udel

Nov 20, 2009, 1:32 AM

Post #7 of 14 (476 views)
Permalink
Re: Is there something similar to list comprehension in dict? [In reply to]

Peng Yu wrote:
> I'm wondering if there is something similar to list comprehension for
> dict (please see the example code below).

Python 3 has list, set, and dict comprehensions.
Don't know about 2.6/7

--
http://mail.python.org/mailman/listinfo/python-list


joost at h-labahn

Nov 20, 2009, 3:08 AM

Post #8 of 14 (472 views)
Permalink
Re: Is there something similar to list comprehension in dict? [In reply to]

NB: I wondered about about dict(one=1, two=2) - why not d = {one:1,
two:2} ? Since you do not write L=list((1, 2)) either. These composed
objects as basic building blocks make Python code so dense and
beautiful, thus using "{}" means embracing the language's concept.
--
http://mail.python.org/mailman/listinfo/python-list


andreengels at gmail

Nov 20, 2009, 3:41 AM

Post #9 of 14 (472 views)
Permalink
Re: Is there something similar to list comprehension in dict? [In reply to]

On Fri, Nov 20, 2009 at 4:18 AM, Peng Yu <pengyu.ut [at] gmail> wrote:
> I'm wondering if there is something similar to list comprehension for
> dict (please see the example code below).
>
>
> d = dict(one=1, two=2)
> print d
>
> def fun(d):#Is there a way similar to list comprehension to change the
> argument d so that d is changed?
>  d=dict(three=3)
>
> fun(d)
> print d
>
> def fun1(d):
>  d['one']=-1
>
> fun1(d)
> print d
>
>
> L = [1, 2]
> print L
>
> def fun2(L):#this doesn't have any effect on the argument L
>  L=[]
>
> fun2(L)
> print L#[1, 2]
>
> def fun3(L):# argument L is changed
>  L[:]=[1, 2, 3]
>
> fun3(L)
> print L#[1, 2, 3]
> --
> http://mail.python.org/mailman/listinfo/python-list
>

def fun(d):
d.clear()
d[three] = 3




--
André Engels, andreengels [at] gmail
--
http://mail.python.org/mailman/listinfo/python-list


davea at ieee

Nov 20, 2009, 3:45 AM

Post #10 of 14 (474 views)
Permalink
Re: Is there something similar to list comprehension in dict? [In reply to]

Peng Yu wrote:
> I'm wondering if there is something similar to list comprehension for
> dict (please see the example code below).
>
>
> d = dict(one=1, two=2)
> print d
>
> def fun(d):#Is there a way similar to list comprehension to change the
> argument d so that d is changed?
> d=dict(three=3)
>
> fun(d)
> print d
>
> def fun1(d):
> d['one']=-1
>
> fun1(d)
> print d
>
>
> L = [1, 2]
> print L
>
> def fun2(L):#this doesn't have any effect on the argument L
> L=[]
>
> fun2(L)
> print L#[1, 2]
>
> def fun3(L):# argument L is changed
> L[:]=[1, 2, 3]
>
> fun3(L)
> print L#[1, 2, 3]
>
>
You confused me by calling it a list comprehension. All you're using in
fun3() is a slice. Using a slice, you can give a new set of values to
an existing list.

For a dictionary, it's just a bit trickier. You need two steps in the
most general case.

def fun4(d):
d.clear() #clear out existing entries
d.update(new_dict) #copy in new key:val pairs from a
different dictionary

This function will modify the caller's dictionary, completely replacing
the contents.

DaveA
--
http://mail.python.org/mailman/listinfo/python-list


mail at timgolden

Nov 20, 2009, 3:47 AM

Post #11 of 14 (471 views)
Permalink
Re: Is there something similar to list comprehension in dict? [In reply to]

Michele Simionato wrote:
> On Nov 20, 4:18 am, Peng Yu <pengyu...@gmail.com> wrote:
>> I'm wondering if there is something similar to list comprehension for
>> dict
>
> Yes, but only in Python 3:
>
>>>> {(i, x) for i, x in enumerate('abc')}
> {(0, 'a'), (1, 'b'), (2, 'c')}

Although the 2.x syntax is hardly onerous:

dict ((i+5, x) for i, x in enumerate ('abc'))


-- obviously without something like the i+5, the example
equates to dict (enumerate ('abc'))

:)

TJG
--
http://mail.python.org/mailman/listinfo/python-list


simon at brunningonline

Nov 20, 2009, 4:00 AM

Post #12 of 14 (473 views)
Permalink
Re: Is there something similar to list comprehension in dict? [In reply to]

2009/11/20 Michele Simionato <michele.simionato [at] gmail>:
> Yes, but only in Python 3:
>
>>>> {(i, x) for i, x in enumerate('abc')}
> {(0, 'a'), (1, 'b'), (2, 'c')}

In Python 2.x, you can do:

>>> dict((i, x) for i, x in enumerate('abc'))
{0: 'a', 1: 'b', 2: 'c'}

(Works in 2.5 - I can't remember when generator expressions were introduced.)

--
Cheers,
Simon B.
--
http://mail.python.org/mailman/listinfo/python-list


deets at nospam

Nov 20, 2009, 6:03 AM

Post #13 of 14 (476 views)
Permalink
Re: Is there something similar to list comprehension in dict? [In reply to]

DreiJane schrieb:
> NB: I wondered about about dict(one=1, two=2) - why not d = {one:1,
> two:2} ? Since you do not write L=list((1, 2)) either. These composed

because it's not working.

>>> {one : 1}
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'one' is not defined


Yes, that looks nitpicky, but that is exactly the reason one often
prefers the dict(...)-variant. Because it uses python keywords, it
spares you to type quotes around all the keys. Which IMHO is more aesthetic.


> objects as basic building blocks make Python code so dense and
> beautiful, thus using "{}" means embracing the language's concept.

The collection-literals are a great thing, no doubt. But these
alternatives are not against any concept.

Diez
--
http://mail.python.org/mailman/listinfo/python-list


steve at REMOVE-THIS-cybersource

Nov 20, 2009, 12:37 PM

Post #14 of 14 (468 views)
Permalink
Re: Is there something similar to list comprehension in dict? [In reply to]

On Fri, 20 Nov 2009 03:08:01 -0800, DreiJane wrote:

> NB: I wondered about about dict(one=1, two=2) - why not d = {one:1,
> two:2} ?

Because it doesn't work unless you have defined names one and two.

dict(one=1, two=2) uses keyword arguments, namely one and two. This is
the same standard mechanism by which you call functions with keyword
arguments:

myfunc(widget=x, width=5, name='fred', flag=True)


The dict literal syntax requires names one and two to already exist,
otherwise you have to quote them to make them strings:

d = {'one': 1, 'two': 2}

> Since you do not write L=list((1, 2)) either.

But you certainly can. It would be wasteful, since first it constructs a
tuple (1, 2), then it creates a list from that tuple.


> These composed
> objects as basic building blocks make Python code so dense and
> beautiful, thus using "{}" means embracing the language's concept.

I don't understand this sentence.



--
Steven
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.