pop()

If you want to see a removed item, you can use the pop () method. The syntax for the method is given as follows:

list.pop()

The pop () method removes and returns the last item from the list.

Let's take the example of the famous TV series, GoT:

>>> GoT = ["Tyrion","Sansa", "Arya","Joffrey","Ned-Stark"]

>>> GoT.pop()

'Ned-Stark'

>>> GoT.pop()

'Joffrey'

>>> GoT

['Tyrion', 'Sansa', 'Arya']

>>>

In the preceding example, you can see that the pop () method returns the removed item. You can also remove any item based on the index. See the following example:

>>> GoT = ["Tyrion","Sansa", "Arya","Catelyn","Joffrey","Ned-Stark"]

>>> GoT = ["Tyrion","Sansa", "Arya","Catelyn","Joffrey","Ned-Stark"]

>>> GoT.pop(3)

'Catelyn'

>>> GoT

['Tyrion', 'Sansa', 'Arya', 'Joffrey', 'Ned-Stark']

>>>

In preceding example, index number 3, that is, "Catelyn" has been removed. Sometimes, you need to sort the values of a list. Python lists offer a very beautiful method called sort ().

The syntax for the method is given as follows:

list.sort(cmp=None, key=None, reverse=False)

The sort() method is stable and inplace. Stable means that the order of items that compare equal will be preserved. Inplace in sort does not use extra memory. Let's understand the sort() method by examples.

Consider the following example where simple numbers are sorted in an ascending order:

>>> list1 = [5, 6, 7, 1, 4, 2, 0, 4, 2, 8]

>>> list1.sort()

>>> list1

[0, 1, 2, 2, 4, 4, 5, 6, 7, 8]

>>>

Let's see how to sort the list in the descending order:

>>> list1 = [5, 6, 7, 1, 4, 2, 0, 4, 2, 8]

>>> list1.sort (reverse=True)

>>> list1

[8, 7, 6, 5, 4, 4, 2, 2, 1, 0]

>>>

Now, you got an idea of the reverse argument. Let's take an example of a list of numbers and strings:

>>> list2 = [8, 7,4,2,1, "1", "a","@#", "nm"]

>>> list2.sort ()

>>> list2

[1, 2, 4, 7, 8, '1', '@#', 'a', 'nm']

For ascending order, the interpreter first takes the number and then strings.

For descending order, the interpreter first takes string, then numbers:

>>>

>>> list2 = [8, 7,4,2,1, "1", "a","@#", "nm"]

>>> list2.sort (reverse=True)

>>> list2

['nm', 'a', '@#', '1', 8, 7, 4, 2, 1]

>>>

Let's take some complex examples:

Consider that you have a list of tuples as follows:

[("a",4),("b",1),("v",5),("f",2)]

The tuples in the list contain two values, and you want to sort the list according to the second value of tuples.

See the following code:

def fun1(x):

return x[1]

list_tup = [("a",4),("b",1),("v",5),("f",2)]

list_tup.sort(key= fun1)

print list_tup

In the preceding code, we defined a fun1() function, which is used as a key of the sort method. The list_tup list passes its elements one by one to the fun1(x) function, and fun1(x) returns the tuple's second element because sort would be performed according to the tuple's second element.

Let's see the output of the program:

Output of sort of list.

I have one more example for you. Consider that you want to sort the elements of a list but based on some conditions. Consider that you have a list as follows:

list1 = [10,9,3,7,2,1,23,1,561,1,1,96,1] 

You want to sort all the elements, but you want that all 1 elements must remain on the right side. It can be accomplished by the cmp argument of the sort() method.

Let's discuss a simple example:

list1 = [10,9,3,7,2,1,23,1,561,1,1,96,1]

def cmp1(x,y):

return

list1.sort(cmp = cmp1)

In the preceding example, we have a list of unsorted numbers. We are using the cmp argument. The cmp1 function takes two arguments. These arguments come from a given list, for example, (10, 9), (9, 3), (3,7), and so on. If cmp1 returns a negative number, then ascending order sort happens, and if it returns a positive number, then descending sort happens.

In our problem, we want ascending order sort, but we also want to push all the 1s to the right:

list1 = [10,9,3,7,2,1,23,1,561,1,1,96,1]

def cmp1(x,y):

if x == 1 or y==1:

c = y-x

else:

c = x-y

return c



list1.sort(cmp = cmp1)

print list1

Here is the output of the program:

Showing output of list cmp argument
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.12.34