Is there something similar to list comprehension in dict?

P

Peng Yu

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]
 
S

Stefan Behnel

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
 
S

Stefan Behnel

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:
{'value': 5}

Stefan
 
D

DreiJane

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.
 
T

Tim Golden

Michele said:
Yes, but only in Python 3:

{(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
 
S

Simon Brunning

2009/11/20 Michele Simionato said:
Yes, but only in Python 3:

{(0, 'a'), (1, 'b'), (2, 'c')}

In Python 2.x, you can do:
{0: 'a', 1: 'b', 2: 'c'}

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

Diez B. Roggisch

DreiJane said:
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.
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
 
S

Steven D'Aprano

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top