Skip to main content

Python List sorting with key argument explored

Everybody uses lists as an array to store values. List provide a lot of in-build features.

  • Sorting
  • Membership
  • Indexing
  • Iteration
  • reversing
  • Adding/Removing
  • Popping
  • Count

Lots of reasons to use lists. I think, one of the most used features is sorting. Internally , python uses Merge sort technique to sort the array items. But the sort method can be used in many other ways to have more control.

The python help says:

>>> help(list.sort)
Help on method_descriptor:

    L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
    cmp(x, y) -> -1, 0, 1

Things to note: The sort method doesn't return anything. It saves the changes in the list permanently. Thats why it is faster than the method sorted


>>> l = ['c', 'b', 'd', 'a']
>>> l.sort()
>>> l
['a', 'b', 'c', 'd']

If elements are strings, they gets sorted by alphabetical order.


>>> l = ['c', 'b', 'd', 'a']
>>> l.sort(reverse=True)
>>> l
['d', 'c', 'b', 'a']

Using the KEY argument.

Perhaps the most versatile of all is the "key" argument. The value of the key parameter should be a function that takes a single argument and it returns a key to use for sorting purposes. This technique is fast because the key function is called exactly once for each input record. 

Simple Sort

>>> l = ['c', 'b', 'd', 'a']
>>> l.sort(key=str, reverse=True)
>>> l
['d', 'c', 'b', 'a']

What above code shows is, I want to sort alphabetically by the rules of ascii , in reverse order. This is not so convincing I assume. Perhaps a much interesting example.

Sort by Length of string

>>> l = ['abc', 'b', 'ab']
>>> l.sort(key=len)
>>> l
['b', 'ab', 'abc']

In the above case, we are sorting by length of strings.

So we actually now have a list : [3, 1, 2] .

Hence the result,

['b', 'ab', 'abc']

Corresponding lengths: 

[1 , 2, 3]

Sort by case (Upper)

>>> l = ['abc', 'A', 'AB']
>>> l.sort(key=str.upper)
>>> l
['A', 'AB', 'abc']

When we specify str.upper, we are saying - Treat every element as UPPER CASE, then sort.

So we are sorting ['ABC', 'A', 'AB']

As a result, we get ['A', 'AB', 'ABC']

Sort by case (Lower)

>>> l = ['abc', 'A', 'B', 'b', 'AB']
>>> l.sort(key=str.lower)
>>> l
['A', 'AB', 'abc', 'B', 'b']

Sort by last letter

>>> strs = ['xc', 'zb', 'yd' ,'wa']
>>> strs.sort(key=lambda x: x[-1])
>>> strs
['wa', 'zb', 'xc', 'yd']

I want to sort by the last letter of each word. Essentially this is how we manually do it.

Step 1 : Extract last letter of each word and keep in a list
Step 2 : Sort that list
Step 3 : Relatively display the original list but sorted.

For the Step 1 , we have used a function created using lambda. This function accepts a string and returns it's last letter.

Zeros as least preference

I have a list of numbers with some zeros in it . I want to have the list sorted , but I also want all the zeros at the end.

Original list : l = [4,0,3,0,8,0,1]

Expected list after sorting: [1, 3, 4, 8, 0, 0, 0]

There are many ways to do it. I'll use what comes to my mind first.

>>> sorted(l, key=lambda x:str(x) if x == 0 else x)
[1, 3, 4, 8, 0, 0, 0]

Guess what's happening here? I am exploiting the fact that, as a preference, python will pick up integers first, then strings. So I converted 0 into '0'.
Here's the proof:

>>> ll = [3,2,3, '1', '3', '0']
>>> sorted(ll)
[2, 3, 3, '0', '1', '3']

Sorting a list of big string integers

I have a list of big huge integers in string form and I want sort that. The idea we can use here is:
We know a 2 length integer is likely to be bigger than a 1 length integer.

>>> l = ['1','2','3','56235434245634562345','556532', '84']
>>> sorted(l, key=lambda x: (len(x), x))
['1', '2', '3', '84', '556532', '56235434245634562345']

Essentially we are sorting a list of tuple.

>>> sorted([(1, 5), (1,2), (2, 0), (2, 4)])
[(1, 2), (1, 5), (2, 0), (2, 4)]

So minimal length numbers will get sorted first. Then next length follows and so on .

Sorting a list of dictionaries by a specific key

We have a list that has many dictionaries . All dicts has the same key . We want to sort the list by the value of this key.

>>> l
[{'a': 60}, {'a': 10}, {'a': 20}]

>>> print sorted(l, key=lambda x:x['a'])
[{'a': 10}, {'a': 20}, {'a': 60}]

Need more guidance? I found the google developers guide to be very good. Have a look.

Popular posts from this blog

Why should you visit Kashmir sooner?

"If there is paradise on Earth, this is it"
And paradise it was . Early winter is the perfect time to plan for a visit to Kashmir. We will suggest to keep your itinerary  short . And enjoy the best trip of your life. 

   My Visit Month : Mid November 2015

As we were flying close to Srinagar, about 20 minutes before landing , all of a sudden , every passenger in the aircraft was on his toes craning theirs necks over the tiny window to catch a glimpse of the above. A fleet of Snow Giants.

   Visit Meena Bazaar in Dal Lake
  You will find a floating village with houses , shops, and hotels on all kinds of boats.

    A night on a House Boat

Take a Shikara ride.Watch the super clear reflection of the mountain. Spot fishes and water birds.

Take a 2 day stay in Pahalgam. Visit Mini Switzerland first day on a horse ride.

Visit Sonemarg. Enjoy the snow.

On your way to Pahalgam, visit apple orchids . Taste all kind of apples. The golden ones are supposed to be the best.

The dark ominous mou…

Kerala : How to plan for a lifetime's experience

I have been planning this for months and it finally came along in the best time. A full stretch Kerala trip with Kanyakumari as an added bonus. I will not tell you what to do, but HOW to do it. How to plan your dream Kerala Trip. 

Caution: Mine was too comprehensive. Needs high energy and will be quite tiring. 

I chose the monsoon theme..I wanted to be in the middle of the action. This is the only place in India which offers monsoon as a tourism opportunity. I was lucky. I expected super heavy rains. Instead. I ended up right in between the rainy season and summer which was Perfect.

Advantages in travelling in mid AugustRainfall almost over.Waterfalls at their fullest.Greenery all over the state.Backwaters, beach sides and rivers all very clean.Abundant coconuts to enjoy.Hotel rates brought down.Less crowd everywhere.Festive Preparation going on.Shopping becomes pocket friendly.Our route map:

Day 1 Day 2 Day 3 Day 4 Day 5 Day 6

CNTLM in Office

If you are facing a proxy problem , CNTLM is a general option everybody considers. But the working of CNTLM is not always guaranteed. I myself get confused every time I need to use it.

What problem are we actually talking about?

While working in offices and restricted environments where firewalls and other invisible barriers stop your requests from going out, you sometimes need a way to temporarily jump over that barrier to get your job done . Example: You might face them while trying to install using pip in command line, trying to clone a personal git repository. 

What is CNTLM exactly?

I think this is best answered by the creator himself. Refer this. You can download it from here. For configuration, you an refer this. There are many uses of CNTLM but I am trying to solve my own problem here. So I will stick to breaking office barriers for now ;) .

Why do I need to use it :
1) Python package installations using PIP
2) Git operations : Cloning and pushing to server.

Here are a set of steps I …