Skip to content
Advertisement

Using filter and lambda in a list of lists

I working with a list of lists. Each of those lists are the same — they contain title, url and some additional statistics (always in the same order).


My list of lists

I would like to create a function find_title, which takes the wanted title and returns the whole list (with title, url and statistics). That’s my attempt

def find_title(title, ls):
    return(list(filter(lambda x: x[0] == title, ls)))

However it doesn’t work, it returns nothing. That’s probably because x[0] denotes only the first element in the big list. How can it be fixed?

Edit. That’s a part of ls:

[['Der Vagabund und das Kind (1921)',
  'http://www.imdb.com/title/tt0012349/',
  0,0,0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
 ['Goldrausch (1925)',
  'http://www.imdb.com/title/tt0015864/',
  0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
 ['Metropolis (1927)',
  'http://www.imdb.com/title/tt0017136/',
  0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0]]

Advertisement

Answer

You can directly extract the information from dataframe like this:

import pandas as pd
df = pd.DataFrame([['Der Vagabund und das Kind (1921)',
  'http://www.imdb.com/title/tt0012349/',
  0,0,0,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
 ['Goldrausch (1925)',
  'http://www.imdb.com/title/tt0015864/',
  0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
 ['Metropolis (1927)',
  'http://www.imdb.com/title/tt0017136/',
  0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0]])

print(df[df[0] =='Goldrausch (1925)'])
1 People found this is helpful
Advertisement