is this pythonic?

alex23 wuwei23 at gmail.com
Wed Jan 21 12:58:57 EST 2009


On Jan 22, 3:34 am, TP <Tribulati... at Paralleles.invalid> wrote:
> >     for index, record in enumerate(l):
> >         if record['title'] == 'ti':
> >             l.pop(index)
>
> Ok, I will use this solution. But it is less pythonic than list
> comprehensions.

Are you asking if it's less pythonic, or asserting? Because you'll
find a lot of disagreement here: list comprehensions are used for
constructing lists, not manipulating them.

> Perhaps you mean rather:
>
> l = [d for d in l if d['title'] != 'ti']
> ?

You are correct. I should never post past 3am :)

> In fact, in my case, in cannot use this simple solution, because there are
> other fields in each dictionary, not only 2. I was not clear in my post.
> So my list is rather:
> l=[{"title":"to", "color":"blue", "value":2}
> {"title":"ti", "color":"red", "value":"coucou"}]

I still find this a lot simpler:

  records = {'to': {'color': 'blue', 'value': '2'}, 'ti': {'color':
'red', 'value': 'coucou'}}

> > It's always better to design for what you know you need, not what you
> > may possibly need in the future.
>
> Ok. Do all the programmers agree with this principle?

Have you seen the size of some of the threads here? It's hard to get
two programmers to agree on variable names... :) But it's a practice
that has served me well and it even has a catchy name[1]. Getting what
you -need- to work is effort enough, if you don't have a definite use
case for a feature then how do you know you've implemented it
correctly? Write it when you need it.

1: http://en.wikipedia.org/wiki/You_Ain%27t_Gonna_Need_It



More information about the Python-list mailing list