Search code examples
pythonloopstextrating

How to get the first 6 texts


for now I have an output with many ratings, and would want to only print out the first sixth in python.

import re
import requests

data = requests.get('https://www.imdb.com/chart/').text
titles = re.findall('/title/\w*/(?=">)', data)
rating = re.findall('\d\.\d.*ratings', data) 

rating

output I got :

['8.8 based on 716,954 user ratings',
 '8.8 based on 1,729,095 user ratings',
 '8.8 based on 1,937,913 user ratings',
 '8.7 based on 1,902,393 user ratings',
 '8.7 based on 2,167,514 user ratings',
 '8.7 based on 1,544,013 user ratings',
 '8.7 based on 1,203,819 user ratings',
 '8.6 based on 1,753,684 user ratings',

Solution

  • You can get the first six items in the list by this:

    your_list[:6]
    

    EDIt

    for i in range(0,len(user_ratings)):
       user_ratings[i] = f"No {i+1}: {user_ratings[i]}" 
    user_ratings[:6]
    

    OR

    user_ratings = [f"No {i+1}: {user_ratings[i]}" for i in range(0, len(user_ratings))]