Re: disgrating a list

2006-09-02 Thread Bryan Olson
Tal Einat wrote: > Tim Chase wrote: >> I'm not sure if '__iter__' is the right thing to be looking for, >> but it seems to work at least for lists, sets, dictionarys (via >> their keys), etc. I would use it because at least then you know >> you can iterate over it > > AFAIK and as seen throughout

Re: disgrating a list

2006-09-02 Thread Hardcoded Software
Tal Einat wrote: > Neil Cerutti wrote: > > On 2006-09-01, Tal Einat <[EMAIL PROTECTED]> wrote: > > > Tim Chase wrote: > > >> I'm not sure if '__iter__' is the right thing to be looking > > >> for, but it seems to work at least for lists, sets, > > >> dictionarys (via their keys), etc. I would use

Re: disgrating a list

2006-09-02 Thread Tal Einat
Neil Cerutti wrote: > On 2006-09-01, Tal Einat <[EMAIL PROTECTED]> wrote: > > Tim Chase wrote: > >> I'm not sure if '__iter__' is the right thing to be looking > >> for, but it seems to work at least for lists, sets, > >> dictionarys (via their keys), etc. I would use it because at > >> least then

Re: disgrating a list

2006-09-01 Thread Simon Forman
jwaixs wrote: > Thank you for all your reply and support. Neil's fits the most to me. I > shrinked it to this function: > > def flatten(x): > for i in range(len(x)): > if isinstance(x[i], list): > x[i:i+1] = x[i] > > Thank you all again. If someone could find even a cuter wa

Re: disgrating a list

2006-09-01 Thread Gimble
Or, (to add needed recursion to your simple loop): def flatten(x): "Modified in-place flattening" for i in range(len(x)): if isinstance(x[i], list): x[i:i+1] = flatten(x[i]) return x This version modifies in-place (as yours does), which may or may not be what you w

Re: disgrating a list

2006-09-01 Thread George Sakkis
jwaixs wrote: > Thank you for all your reply and support. Neil's fits the most to me. I > shrinked it to this function: > > def flatten(x): > for i in range(len(x)): > if isinstance(x[i], list): > x[i:i+1] = x[i] > > Thank you all again. If someone could find even a cuter w

Re: disgrating a list

2006-09-01 Thread Gimble
> def flatten(x): > for i in range(len(x)): > if isinstance(x[i], list): > x[i:i+1] = x[i] I don't think this does what you want. Try [[[1,2,3]]] as a trivial example. A recursive version, that's not as short as yours: def flatten(x): "Recursive example to flatten a

Re: disgrating a list

2006-09-01 Thread Diez B. Roggisch
jwaixs schrieb: > Diez B. Roggisch wrote: >> Why do you wrap a in a list? Just >> >> c = a + [b] >> >> will do it. > > Yes I know, but the problem is I don't know if 'a' is a list or not. I > could make a type check, but I don't want to program in that way. So you're telling us that if not isins

Re: disgrating a list

2006-09-01 Thread jwaixs
Thank you for all your reply and support. Neil's fits the most to me. I shrinked it to this function: def flatten(x): for i in range(len(x)): if isinstance(x[i], list): x[i:i+1] = x[i] Thank you all again. If someone could find even a cuter way, I'd like to see that way.

Re: disgrating a list

2006-09-01 Thread Neil Cerutti
On 2006-09-01, Tal Einat <[EMAIL PROTECTED]> wrote: > Tim Chase wrote: >> I'm not sure if '__iter__' is the right thing to be looking >> for, but it seems to work at least for lists, sets, >> dictionarys (via their keys), etc. I would use it because at >> least then you know you can iterate over i

Re: disgrating a list

2006-09-01 Thread Neil Cerutti
On 2006-09-01, jwaixs <[EMAIL PROTECTED]> wrote: > Hello, > > How can I disgrate (probably not a good word for it) a list? For > example: > > a = [1,2] > b = 3 > c = [a] + [b] # which makes [[1,2],3] > > Then how can I change c ([[1,2],3]) into [1,2,3]? I have a > simple function for this: You

Re: disgrating a list

2006-09-01 Thread Tal Einat
Tim Chase wrote: > I'm not sure if '__iter__' is the right thing to be looking for, > but it seems to work at least for lists, sets, dictionarys (via > their keys), etc. I would use it because at least then you know > you can iterate over it AFAIK and as seen throughout posts on c.l.py, the best

Re: disgrating a list

2006-09-01 Thread Hardcoded Software
Tim Chase wrote: > >>> def flatten(x): > ... q = [] > ... for v in x: > ... if hasattr(v, '__iter__'): > ... q.extend(flatten(v)) > ... else: > ... q.append(v) > ... return q > ... Let's do some nitpicking on "pythonic" s

Re: disgrating a list

2006-09-01 Thread Peter Otten
jwaixs wrote: > > Diez B. Roggisch wrote: >> Why do you wrap a in a list? Just >> >> c = a + [b] >> >> will do it. > > Yes I know, but the problem is I don't know if 'a' is a list or not. I > could make a type check, but I don't want to program in that way. Somewhere in the call chain you have

Re: disgrating a list

2006-09-01 Thread Tim Chase
jwaixs wrote: > Hello, > > How can I disgrate (probably not a good word for it) a list? For > example: > > a = [1,2] > b = 3 > c = [a] + [b] # which makes [[1,2],3] > > Then how can I change c ([[1,2],3]) into [1,2,3]? I have a simple > function for this: > > def fla

Re: disgrating a list

2006-09-01 Thread George Sakkis
jwaixs wrote: > Hello, > > How can I disgrate (probably not a good word for it) a list? For > example: > > a = [1,2] > b = 3 > c = [a] + [b] # which makes [[1,2],3] > > Then how can I change c ([[1,2],3]) into [1,2,3]? I have a simple > function for this: > > def flatt

Re: disgrating a list

2006-09-01 Thread jwaixs
Diez B. Roggisch wrote: > Why do you wrap a in a list? Just > > c = a + [b] > > will do it. Yes I know, but the problem is I don't know if 'a' is a list or not. I could make a type check, but I don't want to program in that way. Noud -- http://mail.python.org/mailman/listinfo/python-list

Re: disgrating a list

2006-09-01 Thread Diez B. Roggisch
jwaixs schrieb: > Hello, > > How can I disgrate (probably not a good word for it) a list? For > example: > > a = [1,2] > b = 3 > c = [a] + [b] # which makes [[1,2],3] Why do you wrap a in a list? Just c = a + [b] will do it. Diez -- http://mail.python.org/mailman/listinfo/python-list

disgrating a list

2006-09-01 Thread jwaixs
Hello, How can I disgrate (probably not a good word for it) a list? For example: a = [1,2] b = 3 c = [a] + [b] # which makes [[1,2],3] Then how can I change c ([[1,2],3]) into [1,2,3]? I have a simple function for this: def flatten(l): r = [] s = [l]