On 04/01/2011 05:33, Paul Rubin wrote:
"Octavian Rasnita" writes:
If I want to create a dictionary from a list...
l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
dict(l[i:i+2] for i in xrange(0,len(l),2))
seems simplest to me.
Or:
dict(zip(l[0 : : 2], l[1 : : 2]))
--
http://mail.python.or
The shorter version:
This doesn't need any x = iter(list) line. perhaps more useful if you
have a bunch of lists to be converted through out your code.
def dictit(lyst):
i = 0
while i < len(lyst):
yield lyst[i], lyst[i+1]
i = i + 2
l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
{
Here is something totally the opposite of a nice one liner:
A hackish module with a bloated generator. Feel free to comment, so I
can learn the errors of my ways. Try not to be too mean though. Try to
think of the attached file as a demonstration of ideas instead of good
coding practices.
Don't b
or only convert the item when you need it leaving the lists as the
source
lyst = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
func = lambda alist, index: dict([(lyst[index*2],
lyst[(index*2)+1]),])
func(lyst, 0)
{1: 2}
func(lyst, 2)
{5: 6}
##
or as a function
def func(lyst, index):
"Octavian Rasnita" writes:
> If I want to create a dictionary from a list...
> l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
dict(l[i:i+2] for i in xrange(0,len(l),2))
seems simplest to me.
--
http://mail.python.org/mailman/listinfo/python-list
An adaptation to Hrvoje Niksic's recipe
Use a dictionary comprehention instead of a list comprehension or
function call:
lyst = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
it = iter( lyst )
dyct = {i:it.next() for i in it} # I'm using {} and not [] for those
with tiny fonts.
#print dyct
{8: 'b', 1: 2,
On Jan 2, 3:18 pm, "Octavian Rasnita" wrote:
> Hi,
>
> If I want to create a dictionary from a list, is there a better way than the
> long line below?
>
> l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
>
> d = dict(zip([l[x] for x in range(len(l)) if x %2 == 0], [l[x] for x in
> range(len(l)) if x %2 ==
On Jan 2, 2011 4:15 PM, "Octavian Rasnita" wrote:
>
>
> Octavian
>
> - Original Message -
> From: "Alex Willmer"
> Newsgroups: comp.lang.python
> To:
> Cc:
> Sent: Sunday, January 02, 2011 8:07 PM
> Subject: Re: list 2 dict?
>
On 2 January 2011 21:04, Octavian Rasnita wrote:
>> No. As Ian said grouper() is a receipe in the itertools documentation.
>>
>> http://docs.python.org/library/itertools.html#recipes
>
> I know that, that is why I used:
>
> from itertools import *
>
> Isn't enough?
Did you follow the link? groupe
Octavian
- Original Message -
From: "Alex Willmer"
Newsgroups: comp.lang.python
To:
Cc:
Sent: Sunday, January 02, 2011 8:07 PM
Subject: Re: list 2 dict?
> On Sunday, January 2, 2011 3:36:35 PM UTC, T wrote:
>> The grouper-way looks nice, but I tried it
Am 02.01.2011 19:19, schrieb Emile van Sebille:
On 1/2/2011 8:31 AM Stefan Sonnenberg-Carstens said...
A last one:
l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
dict((x[1],x[0]) for x in ((l.pop(),l.pop()) for x in xrange(len(l)/2)))
This also works:
l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
pop=l.
On 1/2/2011 8:31 AM Stefan Sonnenberg-Carstens said...
Nevermind -- my bad.
Emile
--
http://mail.python.org/mailman/listinfo/python-list
On 1/2/2011 8:31 AM Stefan Sonnenberg-Carstens said...
A last one:
l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
dict((x[1],x[0]) for x in ((l.pop(),l.pop()) for x in xrange(len(l)/2)))
This also works:
l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
pop=l.pop
dict([(pop(),pop()) for i in l])
--
http://
On Sunday, January 2, 2011 3:36:35 PM UTC, T wrote:
> The grouper-way looks nice, but I tried it and it didn't work:
>
> from itertools import *
> ...
> d = dict(grouper(2, l))
>
> NameError: name 'grouper' is not defined
>
> I use Python 2.7. Should it work with this version?
No. As Ian said g
Am 02.01.2011 16:36, schrieb Octavian Rasnita:
From: "Ian Kelly"
On 1/2/2011 6:18 AM, Octavian Rasnita wrote:
Hi,
If I want to create a dictionary from a list, is there a better way than the
long line below?
l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
d = dict(zip([l[x] for x in range(len(l))
From: "Rob Williscroft"
> Octavian Rasnita wrote in news:0db6c288b2274dbba5463e7771349...@teddy in
> gmane.comp.python.general:
>
>> Hi,
>>
>> If I want to create a dictionary from a list, is there a better way
>> than the long line below?
>>
>> l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
>>
>>
From: "Ian Kelly"
> On 1/2/2011 6:18 AM, Octavian Rasnita wrote:
>> Hi,
>>
>> If I want to create a dictionary from a list, is there a better way than the
>> long line below?
>>
>> l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
>>
>> d = dict(zip([l[x] for x in range(len(l)) if x %2 == 0], [l[x] for x
Am 02.01.2011 14:18, schrieb Octavian Rasnita:
Hi,
If I want to create a dictionary from a list, is there a better way than the
long line below?
l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
d = dict(zip([l[x] for x in range(len(l)) if x %2 == 0], [l[x] for x in
range(len(l)) if x %2 == 1]))
print
"Octavian Rasnita" writes:
> If I want to create a dictionary from a list, is there a better way than the
> long line below?
>
> l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
>
> d = dict(zip([l[x] for x in range(len(l)) if x %2 == 0], [l[x] for x in
> range(len(l)) if x %2 == 1]))
>
> print(d)
>
> {8
Octavian Rasnita wrote in news:0db6c288b2274dbba5463e7771349...@teddy in
gmane.comp.python.general:
> Hi,
>
> If I want to create a dictionary from a list, is there a better way
> than the long line below?
>
> l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
>
> d = dict(zip([l[x] for x in range(len(l)
Am 02.01.2011 14:18, schrieb Octavian Rasnita:
l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
dict(zip(l[0::2],l[1::2]))
{8: 'b', 1: 2, 3: 4, 5: 6, 7: 'a'}
--
http://mail.python.org/mailman/listinfo/python-list
On 1/2/2011 6:18 AM, Octavian Rasnita wrote:
Hi,
If I want to create a dictionary from a list, is there a better way than the
long line below?
l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
d = dict(zip([l[x] for x in range(len(l)) if x %2 == 0], [l[x] for x in
range(len(l)) if x %2 == 1]))
d = di
Hi,
If I want to create a dictionary from a list, is there a better way than the
long line below?
l = [1, 2, 3, 4, 5, 6, 7, 'a', 8, 'b']
d = dict(zip([l[x] for x in range(len(l)) if x %2 == 0], [l[x] for x in
range(len(l)) if x %2 == 1]))
print(d)
{8: 'b', 1: 2, 3: 4, 5: 6, 7: 'a'}
Thanks.
23 matches
Mail list logo