0

When making my code for NewsAPI, I am receiving almost 800 values. Within these 800 values, only 4 or so are non-duplicates depending on the keyword.

Is there anyway to stop duplicates from generating?

Here is my code :

news_sources = newsapi.get_sources()

for source in news_sources['sources']:
    #print(source['name'])

    all_articles = newsapi.get_everything(
        q='shooting',
        language='en',
        #from_param='2023-01-22',
        #to='2023-01-22'
    )
    for article in all_articles['articles']:
        #print('Source : ', article['source']['name'])
        #print('Title : ', article['title'])
        #print('Date : ', article['publishedAt'])
        print('Url : ', article['url'], '\n\n')

I saw a previous answer to this question, however, not sure if this is applicable to my code

var isDuplicated = false;
for (var new in news) {
   if (new.title == articleModel.title) {
      isDuplicated = true;
   }
}

if (!isDuplicated) {
   // Now you can add it
   news.add(articleModel);
}
endive1783
  • 827
  • 1
  • 8
  • 18

1 Answers1

0

convert it into a set and retrieve back to list

unique_sources = list(set([source['name'] for source in news_sources['sources']]))
shamnad sherief
  • 552
  • 5
  • 17