5
\$\begingroup\$

I would like to display this data, callable at any time, but I'm not sure if this is the best practice to do so.

countries = [
   dict(Name = "China", Population = "1,363,560,000"),
   dict(Name = "India", Population = "1,242,070,000"),
   dict(Name = "United States", Population = "317,768,000"),
   dict(Name = "Indonesia", Population = "249,866,000"),
   dict(Name = "Brazil", Population = "201,032,714"),
   dict(Name = "Pakistan", Population = "186,015,000"),
   dict(Name = "Nigeria", Population = "173,615,000"),
   dict(Name = "Bangladesh", Population = "152,518,015"),
   dict(Name = "Russia", Population = "143,700,000"),
   dict(Name = "Japan", Population = "127,120,000"),
   dict(Name = "Mexico", Population = "119,713,203"),
   dict(Name = "Philippines", Population = "99,329,000"),
   dict(Name = "Vietnam", Population = "89,708,900"),
   dict(Name = "Egypt", Population = "86,188,600"),
   dict(Name = "Germany", Population = "80,716,000"),
   dict(Name = "Iran", Population = "77,315,000"),
   dict(Name = "Turkey", Population = "76,667,864"),
   dict(Name = "Thailand", Population = "65,926,261"),
   dict(Name = "France", Population = "65,844,000"),
   dict(Name = "United Kingdom", Population = "63,705,000"),
   dict(Name = "Italy", Population = "59,996,777"),
   dict(Name = "South Africa", Population = "52,981,991"),
   dict(Name = "South Korea", Population = "50,219,669"),
   dict(Name = "Colombia", Population = "47,522,000"),
   dict(Name = "Spain", Population = "46,609,700"),
   dict(Name = "Ukraine", Population = "45,410,071"),
   dict(Name = "Kenya", Population = "44,354,000"),
   dict(Name = "Argentina", Population = "40,117,096"),
   dict(Name = "Poland", Population = "38,502,396"),
   dict(Name = "Sudan", Population = "37,964,000"),
   dict(Name = "Uganda", Population = "35,357,000"),
   dict(Name = "Canada", Population = "35,344,962"),
   dict(Name = "Iraq", Population = "34,035,000"),
   ]

print countries 
\$\endgroup\$
1
  • \$\begingroup\$ And what, exactly, does this question have to do with code review? Nothing! Is our new policy that anything goes as long as it is easy to answer and tangentially includes the words 'best practice'? This code does not display the data; the question is clearly about how solve the problem, not how to improve the existing code. \$\endgroup\$
    – Adam
    Apr 29, 2014 at 21:41

2 Answers 2

7
\$\begingroup\$

To format the data nicely for printing you can use the ljust method:

print "Country".ljust(15),"Population"
for country in countries:
    print country["Name"].ljust(15), country["Population"]
\$\endgroup\$
1
  • \$\begingroup\$ Thanks a ton. I'll eventually have my game on here I'm building with this for review. \$\endgroup\$
    – tda
    Mar 30, 2014 at 2:23
2
\$\begingroup\$

Starting with the data structure you have (a list of dicts), whose entries are already sorted in descending order of population, it appears that you want the output as a table.

The country name should probably be left justified. The population, being numeric, should probably be right justified. You'll have to decide on some arbitrary column widths.

Python has built in formatting capability to accomplish this, with the ability to extract fields out of a dictionary.

You could define a function that is callable from anywhere.

def print_countries(countries):
    for c in countries:
        print "%(Name)-16s%(Population)16s" % (c)

For flexibility, you could define a stringifying function instead of a printing function.

def countries_to_str(countries):
    return "\n".join(["%(Name)-16s%(Population)16s" % (c) for c in countries])

Then you have the flexibility to output the result of countries_to_str(countries) anywhere you want, such as a file.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.