I'm trying to port this little F# snippet while staying pythonic:
["something"; "something else"; "blah"; "a string"] |> List.map (fun p -> p, p.Length) |> List.sortBy snd
In case you don't speak F#, it gets the length of each string, then sorts by length.
Output:
[("blah", 4); ("a string", 8); ("something", 9); ("something else", 14)]
In Python, this is the best I could do so far:
sorted([(p, len(p)) for p in ["something", "something else", "blah", "a string"]], key=lambda a:a[1])
While correct, this doesn't look very elegant to me, or maybe it's just my non-pythonic eye.
Is this pythonic code? How would you write it? Maybe an imperative style is more appropriate?