Searching for an number in a list with strings, to return the number value

Stack Overflow Asked by Ossz on December 3, 2020

So I have a list of movies containing information on name, imdb rating and category as follows:

movies = [
{
"name": "Usual Suspects",
"imdb": 7.0,
"category": "Thriller"
},
{
"name": "Hitman",
"imdb": 6.3,
"category": "Action"
},


I’m trying to create a function to search in each item of the list for the number decimal i.e. the imdb rating, to compare it to a threshold value (>5.5)

The method I was thinking was to search for each item in this list when the number occurs and returning this value as I go through this list.

If I wanted to go about this method how should I tackle it? I’ve been looking at ways to return a value from lists but the methods I’m finding are for already known items returning an index…

Each position in your list are a dictionary, to acess the 'imdb' you need to do this:

# For each movie in the movies list
for movie in movies:
# 'imdb' is a key of your dictionary
if movie['imdb'] > 5.5: # The movie['imdb'] returns the value (the decimal number)
return True # You can add to another data structure to store for example


Correct answer by Juliano on December 3, 2020

movies = [
{
"name": "Usual Suspects",
"imdb": 7.0,
"category": "Thriller"
},
{
"name": "Hitman",
"imdb": 6.3,
"category": "Action"
}]
print([x['name'] for x in movies if x['imdb']>5.5])


Answered by Janith on December 3, 2020

Since movies is a list of dictionaries, you could just loop through it if you don't have a massive dataset.

ls = []
for movie in movies:
if movie['imdb'] > 5.5:
ls.append(movie)


Answered by Jeff on December 3, 2020

for movie in movies:
imdb_score = movie['imdb']
if imdb_score > 5.5:
print(imdb_score)


Would this work for what you are trying to do?

Answered by yibo on December 3, 2020

Related Questions

How to convet this data to array?(Typescript )

1  Asked on December 23, 2021 by pyarel

Facing issue when simpling runing the build and not pressing cleaning issue

1  Asked on December 23, 2021 by bthakkar

Why is the user defined time’s location nil

1  Asked on December 23, 2021 by kelly-flet

Is it okay to use Flutter for Hipaa compliant app?

1  Asked on December 23, 2021 by iducool

Python – BeautifulSoup – Scraped content only being written to first text file, not subsequent files

1  Asked on December 23, 2021 by brendan-rodgers

3  Asked on December 23, 2021 by frankfurt

Call this.Hide() but the Form is still activated on Win10

0  Asked on December 23, 2021 by jonney

How do you remotely update Java applications?

9  Asked on December 23, 2021 by david-crow

Google Drive API Console: Error saving Drive UI integration page

2  Asked on December 23, 2021 by marco-carnazzo

How can set Formik isSubmitting outside of my component when testing?

2  Asked on December 23, 2021 by user3096803

OAuth2.0 authorization code flow in PowerShell

3  Asked on December 23, 2021

Can you have an embedded registration with universal MFA set up without needing to login to Auth0?

0  Asked on December 23, 2021 by dees-oomens

how to embed plot in gtk and glade

1  Asked on December 23, 2021 by user169808

Link a third party application account in Cognito

1  Asked on December 23, 2021 by cartman

Building static HTML for pageserror: Error: ENOENT: no such file or directory

3  Asked on December 23, 2021 by ericgit

Multivariate normal density in Python?

10  Asked on December 23, 2021 by benno

Allow customer to change the order status in WooCommerce My account

2  Asked on December 23, 2021

onBlur for div element in React

1  Asked on December 23, 2021 by neel-dsouza

Uncaught SyntaxError: Unexpected token ‘<' When I use libarchive.js

2  Asked on December 23, 2021