Monday, 15 February 2010

Splitting a Python list based on criteria -



Splitting a Python list based on criteria -

i have python list follows:

mylist = [('item a','ca','10'),('item b','ct','12'),('item c','ca','14')]

i split list based on column 2 == 'ca'

desired output:

filtered_list = [('item a','ca','10'),('item c','ca','14')]

my attempt: there issues here!

mylist = [('item a','ca','10'),('item b','ct','12'),('item c','ca','14')] filtered_list[] row in mylist: if [row:1] = 'ca' filtered_list.append(mylist[row])

you can utilize list comprehension accomplish this:

mylist = [('item a','ca','10'),('item b','ct','12'),('item c','ca','14')] filtered_list = [item item in mylist if item[1]=='ca']

python list python-2.7

No comments:

Post a Comment