469,336 Members | 5,156 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

about dictionary

I have have the following code:
a=[3,5,8,0]
b={}

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!
Nov 22 '05 #1
20 1759
b = dict([(x,dict()) for x in a])
Shi Mu wrote:
I have have the following code:
a=[3,5,8,0]
b={}

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!


Nov 22 '05 #2
b = dict([(x,dict()) for x in a])
Shi Mu wrote:
I have have the following code:
a=[3,5,8,0]
b={}

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!


Nov 22 '05 #3
On 20 Nov 2005 02:59:30 -0800, bo****@gmail.com <bo****@gmail.com> wrote:
b = dict([(x,dict()) for x in a])
Shi Mu wrote:
I have have the following code:
>> a=[3,5,8,0]
>> b={}
>>

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!

Thanks!!!
Nov 22 '05 #4
On 20 Nov 2005 02:59:30 -0800, bo****@gmail.com <bo****@gmail.com> wrote:
b = dict([(x,dict()) for x in a])
Shi Mu wrote:
I have have the following code:
>> a=[3,5,8,0]
>> b={}
>>

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!

Thanks!!!
Nov 22 '05 #5
Shi Mu <sa************@gmail.com> wrote:
I have have the following code:
a=[3,5,8,0]
b={}

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}


Explicit:

a = [3, 5, 8, 0]
b = {}
for key in a:
b[key] = []

Quicker:

a = [3, 5, 8, 0]
b = dict( [(k, []) for k in a] )

What behaviour do you expect when there are repeated values in the
list 'a'?

--
\ "Faith may be defined briefly as an illogical belief in the |
`\ occurrence of the improbable." -- Henry L. Mencken |
_o__) |
Ben Finney
Nov 22 '05 #6
Shi Mu <sa************@gmail.com> wrote:
I have have the following code:
a=[3,5,8,0]
b={}

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}


Explicit:

a = [3, 5, 8, 0]
b = {}
for key in a:
b[key] = []

Quicker:

a = [3, 5, 8, 0]
b = dict( [(k, []) for k in a] )

What behaviour do you expect when there are repeated values in the
list 'a'?

--
\ "Faith may be defined briefly as an illogical belief in the |
`\ occurrence of the improbable." -- Henry L. Mencken |
_o__) |
Ben Finney
Nov 22 '05 #7

Uzytkownik "Shi Mu" <sa************@gmail.com> napisal w wiadomosci
news:ma**************************************@pyth on.org...
I have have the following code:
a=[3,5,8,0]
b={}

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!
Other solution:
b.fromkeys(a,[])

cheers,

Przemek
Nov 22 '05 #8

Uzytkownik "Shi Mu" <sa************@gmail.com> napisal w wiadomosci
news:ma**************************************@pyth on.org...
I have have the following code:
a=[3,5,8,0]
b={}

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!
Other solution:
b.fromkeys(a,[])

cheers,

Przemek
Nov 22 '05 #9
przemek drochomirecki wrote:
Uzytkownik "Shi Mu" <sa************@gmail.com> napisal w wiadomosci
news:ma**************************************@pyth on.org...
I have have the following code:
a=[3,5,8,0]
b={}
How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!
Other solution:
b.fromkeys(a,[])


Be warned that all keys share the same value.
a = [3, 5, 8, 0]
b = dict.fromkeys(a, [])
b[3].append(42)
b

{8: [42], 0: [42], 3: [42], 5: [42]}

Probably not what you want...

Peter

Nov 22 '05 #10
przemek drochomirecki wrote:
Uzytkownik "Shi Mu" <sa************@gmail.com> napisal w wiadomosci
news:ma**************************************@pyth on.org...
I have have the following code:
a=[3,5,8,0]
b={}
How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!
Other solution:
b.fromkeys(a,[])


Be warned that all keys share the same value.
a = [3, 5, 8, 0]
b = dict.fromkeys(a, [])
b[3].append(42)
b

{8: [42], 0: [42], 3: [42], 5: [42]}

Probably not what you want...

Peter

Nov 22 '05 #11
On 11/20/05, Peter Otten <__*******@web.de> wrote:
przemek drochomirecki wrote:
Uzytkownik "Shi Mu" <sa************@gmail.com> napisal w wiadomosci
news:ma**************************************@pyth on.org...
I have have the following code:
> a=[3,5,8,0]
> b={}
>

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!
Other solution:
b.fromkeys(a,[])


Be warned that all keys share the same value.
a = [3, 5, 8, 0]
b = dict.fromkeys(a, [])
b[3].append(42)
b

{8: [42], 0: [42], 3: [42], 5: [42]}

Probably not what you want...

Peter

how to do with it?
Nov 22 '05 #12
On 11/20/05, Peter Otten <__*******@web.de> wrote:
przemek drochomirecki wrote:
Uzytkownik "Shi Mu" <sa************@gmail.com> napisal w wiadomosci
news:ma**************************************@pyth on.org...
I have have the following code:
> a=[3,5,8,0]
> b={}
>

How I can i assign each item in a as the key in the dictionary b
simultaneously?
that is,
b={3:[],5:[],8:[],0:[]}
Thanks!
Other solution:
b.fromkeys(a,[])


Be warned that all keys share the same value.
a = [3, 5, 8, 0]
b = dict.fromkeys(a, [])
b[3].append(42)
b

{8: [42], 0: [42], 3: [42], 5: [42]}

Probably not what you want...

Peter

how to do with it?
Nov 22 '05 #13
Shi Mu wrote:
how to do with it?


Use Ben Finney's, not Przemek's approach if the values are mutables that you
plan to modify. If that's what you are asking.

Peter
Nov 22 '05 #14
Shi Mu wrote:
how to do with it?


Use Ben Finney's, not Przemek's approach if the values are mutables that you
plan to modify. If that's what you are asking.

Peter
Nov 22 '05 #15

Uzytkownik "Peter Otten" <__*******@web.de> napisal w wiadomosci
news:dl*************@news.t-online.com...
Shi Mu wrote:
how to do with it?
Use Ben Finney's, not Przemek's approach if the values are mutables that

you plan to modify. If that's what you are asking.

Peter


Maybe he just want to use dictionary as a set.
He can use SET container instead.

Przemek
Nov 22 '05 #16

Uzytkownik "Peter Otten" <__*******@web.de> napisal w wiadomosci
news:dl*************@news.t-online.com...
Shi Mu wrote:
how to do with it?
Use Ben Finney's, not Przemek's approach if the values are mutables that

you plan to modify. If that's what you are asking.

Peter


Maybe he just want to use dictionary as a set.
He can use SET container instead.

Przemek
Nov 22 '05 #17
On 11/20/05, przemek drochomirecki <dr*****@wytnij.hyene.com> wrote:

Uzytkownik "Peter Otten" <__*******@web.de> napisal w wiadomosci
news:dl*************@news.t-online.com...
Shi Mu wrote:
how to do with it?


Use Ben Finney's, not Przemek's approach if the values are mutables that

you
plan to modify. If that's what you are asking.

Peter


Maybe he just want to use dictionary as a set.
He can use SET container instead.

Przemek

How to use SET?
Nov 22 '05 #18
On 11/20/05, przemek drochomirecki <dr*****@wytnij.hyene.com> wrote:

Uzytkownik "Peter Otten" <__*******@web.de> napisal w wiadomosci
news:dl*************@news.t-online.com...
Shi Mu wrote:
how to do with it?


Use Ben Finney's, not Przemek's approach if the values are mutables that

you
plan to modify. If that's what you are asking.

Peter


Maybe he just want to use dictionary as a set.
He can use SET container instead.

Przemek

How to use SET?
Nov 22 '05 #19

Uzytkownik "Shi Mu" <sa************@gmail.com> napisal w wiadomosci
news:ma**************************************@pyth on.org...
On 11/20/05, przemek drochomirecki <dr*****@wytnij.hyene.com> wrote:

Uzytkownik "Peter Otten" <__*******@web.de> napisal w wiadomosci
news:dl*************@news.t-online.com...
Shi Mu wrote:
how to do with it?


Use Ben Finney's, not Przemek's approach if the values are mutables that

you
plan to modify. If that's what you are asking.

Peter


Maybe he just want to use dictionary as a set.
He can use SET container instead.

Przemek

How to use SET?
http://docs.python.org/lib/types-set.html
a [3, 5, 8, 8] b = set(a)
b set([8, 3, 5])


cheers,
przemek
Nov 22 '05 #20

Uzytkownik "Shi Mu" <sa************@gmail.com> napisal w wiadomosci
news:ma**************************************@pyth on.org...
On 11/20/05, przemek drochomirecki <dr*****@wytnij.hyene.com> wrote:

Uzytkownik "Peter Otten" <__*******@web.de> napisal w wiadomosci
news:dl*************@news.t-online.com...
Shi Mu wrote:
how to do with it?


Use Ben Finney's, not Przemek's approach if the values are mutables that

you
plan to modify. If that's what you are asking.

Peter


Maybe he just want to use dictionary as a set.
He can use SET container instead.

Przemek

How to use SET?
http://docs.python.org/lib/types-set.html
a [3, 5, 8, 8] b = set(a)
b set([8, 3, 5])


cheers,
przemek
Nov 22 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by John Mudd | last post: by
2 posts views Thread by Shi Mu | last post: by
90 posts views Thread by Christoph Zwerschke | last post: by
5 posts views Thread by Andrew Robinson | last post: by
3 posts views Thread by wildThought | last post: by
37 posts views Thread by Hilton | last post: by
8 posts views Thread by Bob Altman | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.