Question
The following code extracts the city populations that are larger than 10000 from the list of lists populationsSK and stores them in a new list
The following code extracts the city populations that are larger than 10000 from the list of lists populationsSK and stores them in a new list citiesSK.
populationsSK = [ [ Pelican Narrows , 2703] ,
[ Saskatoon , 222035] ,
[ Moose Jaw , 33617] ,
[ La Ronge , 5905] ]
citiesSK = []
for x in populationsSK :
if x [1] >= 10000:
citiesSK . append ( x )
At the end of the program, citiesSK is the list: [[Saskatoon, 222035], [Moose Jaw, 33617]]
(a) Modify the code so that it is an equivalent program in which the list citiesSK is created using a list comprehension instead of a loop.
(b) Using a similar list comprehension, write an expression that creates a list of all the towns whose population is smaller than 40000.
(c) Using a list comprehension, write an expression that creates a list of the all the names of the cities and towns (the new list should contain all the names, but no population data)
(d) Using a list comprehension, write an expression that creates a list of all the towns and cities whose names are more than 8 characters (including blanks).
(This is Python Programming)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started