Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
startswith() function
#1
hi, im new to coding hence i have a rather simple problem!
im trying to sort a list of cities, so that for cities that start with a letter greater than Q, the city name is removed from the list.
My code runs but i dont seem to get the correct output.
How can i specify startswith greater than Q?
many thanks for your help.


visited_cities = ["New York", "Shanghai", "Munich", "Toyko", "Dubai", "Mexico City", "São Paulo", "Hyderabad"]
sorted_cities=sorted(visited_cities)
print(sorted_cities)

for x in sorted_cities:
    if x>="Q":
        sorted_cities.remove(x)
    else:
        pass
print(sorted_cities)
Reply


Messages In This Thread
startswith() function - by milo204 - Mar-05-2018, 03:57 PM
RE: startswith() function - by mpd - Mar-05-2018, 04:48 PM
RE: startswith() function - by DeaD_EyE - Mar-05-2018, 04:57 PM
RE: startswith() function - by milo204 - Mar-06-2018, 10:07 AM
RE: startswith() function - by DeaD_EyE - Mar-06-2018, 10:38 AM

Forum Jump:

User Panel Messages

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