InPython Create a list of strings dont ask from the user and
InPython,
Create a list of strings, don’t ask from the user, and return a list with the strings in sorted order, except group all the strings that begin with \'x\' first.
e.g. [\'mix\', \'xyz\', \'apple\', \'xanadu\', \'aardvark\'] yields
[\'xanadu\', \'xyz\', \'aardvark\', \'apple\', \'mix\']
# Hint: this can be done by making 2 lists and sorting each of them before combining them.
Solution
list1 = [\'mix\', \'xyz\', \'apple\', \'xanadu\', \'aardvark\']
print list1
list2 = []
list3 = []
for i in range(len(list1)):
if list1[i].startswith(\'x\'):
list2.append( list1[i])
else:
list3.append( list1[i])
list3.sort()
list2.sort()
list1 = []
list1.extend(list2)
list1.extend(list3)
print list1
Output:
sh-4.3$ python main.py
[\'mix\', \'xyz\', \'apple\', \'xanadu\', \'aardvark\']
[\'xanadu\', \'xyz\', \'aardvark\', \'apple\', \'mix\']
