I’m attempting to replicate a c# LINQ foreach statement in python. I’m sure there is a better way.
Let’s say I have (in c#):
public class TestData
{
public int Id { get; set; }
public string Something { get; set; }
}
void MyMethod()
{
List<TestData> myList = new List<TestData>();
foreach (var i in myList.Where(x => x.Id > 5))
{
//do something
}
}
I’d like to do something exactly like this in python (2.7). All I need is the foreach loop. Everything else I’ve got down.
Can anyone point me in the right direction?
Yep, it’s very easy. The thing with the square braces is called a List Comprehension.