Python Forum
# only print cities that names start "Q" or earlier
Thread Rating:
  • 1 Vote(s) - 2 Average
  • 1
  • 2
  • 3
  • 4
  • 5
# only print cities that names start "Q" or earlier
#1
my question here
print cities from visited_cities list in alphbetical order using .sort()
# only print cities that names start "Q" or earlier
visited_cities = ["New York", "Shanghai", "Munich", "Toyko", "Dubai", "Mexico City", "São Paulo", "Hyderabad","Qatar"]
visited_cities.sort()
print(visited_cities)
new_cities=[]
for cities in visited_cities:
    if cities[0]=="A"or"B"or"C"or"D"or"E"or"F"or"G"or"H"or"I"or"J"or"K"or"L"or"M"or"N"or"O"or"P"or"Q":
        new_cities.append(cities)
print(new_cities)
But I am getting following output:
['Dubai', 'Hyderabad', 'Mexico City', 'Munich', 'New York', 'Qatar', 'Shanghai', 'São Paulo', 'Toyko']
['Dubai', 'Hyderabad', 'Mexico City', 'Munich', 'New York', 'Qatar', 'Shanghai', 'São Paulo', 'Toyko']

But Desired output is:
['Dubai', 'Hyderabad', 'Mexico City', 'Munich', 'New York', 'Qatar']
Reply


Messages In This Thread
# only print cities that names start "Q" or earlier - by nikhilkumar - Jul-17-2017, 06:20 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Start print a text after open an async task via button Nietzsche 0 808 May-15-2023, 06:52 AM
Last Post: Nietzsche
  Print names in x-axis of a time-series values hobbyist 4 1,398 Apr-22-2023, 09:29 PM
Last Post: deanhystad
  2 versions of module installed, how to import the earlier one sveto4ka 8 5,258 Oct-22-2019, 02:51 PM
Last Post: snippsat
  What's the difference b/w assigning start=None and start=" " Madara 1 2,433 Aug-06-2018, 08:23 AM
Last Post: buran
  How to print the names of assignments if they are randomly selected Kongurinn 2 3,491 Oct-22-2017, 07:31 AM
Last Post: buran

Forum Jump:

User Panel Messages

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