[Tutor] sorted function

D.V.N.Sarma డి.వి.ఎన్.శర్మ dvnsarma at gmail.com
Fri Apr 14 19:46:50 EDT 2017


Change your code to

def front_x(words):
  # +++your code here+++
  ls=[]
  ls1=[]
  for str in words:
      if str[0]=='x':
          ls.append(str)
      else:
          ls1.append(str);
  print ls
  print ls1
  ls =  sorted(ls)
  ls1 = sorted(ls1)
  ls.extend(ls1)
  return ls

regards,
Sarma.

On Fri, Apr 14, 2017 at 11:59 PM, shubham goyal <skgoyal721 at gmail.com>
wrote:

> Dear mentors,
> sorted function is not working when i am trying to sort the list of strings
> but list.sort() is working. can you please help me understand.In this
> question i was trying to sort the list but first sorting the letter
> starting from x and taking them first.
>
> def front_x(words):
>   # +++your code here+++
>   ls=[]
>   ls1=[]
>   for str in words:
>       if str[0]=='x':
>           ls.append(str)
>       else:
>           ls1.append(str);
>   print ls
>   print ls1
>   sorted(ls)
>   sorted(ls1)
>   ls.extend(ls1)
>   return ls
>
> front_x(['bbb', 'ccc', 'axx', 'xzz', 'xaa'])
>
> Output:['xzz', 'xaa', 'bbb', 'ccc', 'axx']
> Output should be:  ['xaa', 'xzz', 'axx', 'bbb', 'ccc']
>
> Thankyou. Sorry for bad writing skills.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list