Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Function Improvement
#1
Hey

I am looking to improve my working function below;

The objective is to get back a list of countries from the free api.
The argument parsed is the number of countries the user wants back , the default is ALL the countries .

Two ways i want to improve is ;

a)i want the len(z) , the maximum value to be defined within the loop instead of explicitly defining it as 250
this would be in case countries are added
b) I want an object list 'country_list' , which is appended to in this code, to be created by this function , and not only just as output displayed when running the function

--------start of function -----------------------

def list_of_countries(number_to_return=250):

    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()
    
    i=0
    while i < lim :
        y=z[i]
        country_list.append(y['name'])
        i+=1 
        
    return  country_list
-------------------------end of function---------------------


test case 1 : list_of_countries(4)
test case 2 : list_of_countries() #without explicitly using 250 in default
test case 3 : print(country_list)
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,052 Aug-01-2021, 11:09 PM
Last Post: saidc
  Name Mashup Program Improvement in Python rhat398 3 2,565 Apr-05-2020, 12:09 PM
Last Post: perfringo
  first try with python class, suggestion for improvement please anna 18 5,937 Nov-01-2019, 11:16 AM
Last Post: anna
  coding improvement tips vaisesumit29 1 1,816 Mar-10-2019, 05:09 PM
Last Post: stullis
  Web Scraping efficiency improvement HiImNew 0 2,392 Jun-01-2018, 08:52 PM
Last Post: HiImNew
  Router interface status through SNMP - suggestion required for improvement. anna 0 3,032 Feb-27-2018, 11:10 AM
Last Post: anna
  working code, suggestion required for improvement anna 18 8,502 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