How to map 2 lists with comparison in python

I have a list of objects of say foos. I have a loop for creating a new list.

foo1 = {id:1,location:2} for e.g. foos = [foo1,foo2,foo3]

Now I want to create a new list based on location.

new_list = []
for foo in foos:
  if foo.location==2:

What I want to know is there any way in which I can do something like this

new_list = []
new_list = map(if foo.location ==2,foos) // this is wrong code but is something like this possible. ?

Can I use map function here ? if yes how ?


Sure thing you can do it with function. You can use the filter builtin function:

new_list = filter(lambda foo: foo.location == 2, foos)

But the more general and "pythonic" way is to use list comprehensions

new_list = [foo for foo in foos if foo.location == 2]

Need Your Help

Consecutive dates postgresql

sql postgresql gaps-and-islands

I need to know if there are no consecutive dates per document. I have this table:

JQUERY FIND:FIRST not finding the first Paragraph?

javascript jquery css-selectors find

Goal, is to extract the content for the CKEDITOR Text Editor, and then only obtain the FIRST paragraph. For some reason the bellow isn't working... Ideas?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.