Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function Improvement
#2
This should help. I simplified the code some, set your country limit to an arbitrarily high number (one would hope there will never be 100,000 countries), limit works,

I am not sure what you want for the second part. The function returns a list object. You can assign that list to another name, copy the list, do whatever you like. Can you clarify what you wanted for the second part?
import urllib
import json

def list_of_countries(number_to_return=100000):
 
    allurl='https://restcountries.eu/rest/v2/all'
 
    uh=urllib.request.urlopen(allurl)
    data = uh.read().decode()
    z=json.loads(data)
     
    lim=number_to_return 
    country_list=list()
     
    for country in z :
        country_list.append(country['name'])
        lim -= 1
        if lim == 0:
            break 
    return  country_list
print(list_of_countries())
Reply


Messages In This Thread
Function Improvement - by Santino - May-23-2020, 10:17 AM
RE: Function Improvement - by jefsummers - May-23-2020, 03:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Random coordinate generator speed improvement saidc 0 2,077 Aug-01-2021, 11:09 PM
Last Post: saidc
  Name Mashup Program Improvement in Python rhat398 3 2,624 Apr-05-2020, 12:09 PM
Last Post: perfringo
  first try with python class, suggestion for improvement please anna 18 6,091 Nov-01-2019, 11:16 AM
Last Post: anna
  coding improvement tips vaisesumit29 1 1,855 Mar-10-2019, 05:09 PM
Last Post: stullis
  Web Scraping efficiency improvement HiImNew 0 2,419 Jun-01-2018, 08:52 PM
Last Post: HiImNew
  Router interface status through SNMP - suggestion required for improvement. anna 0 3,071 Feb-27-2018, 11:10 AM
Last Post: anna
  working code, suggestion required for improvement anna 18 8,638 Dec-29-2017, 01:24 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020