Featured

    Featured Posts

List In Python Part-2


List methods





§  append(value) – appends a new element to the end of the list.



list=[10,20,30]

# Append values 40, 50, and 60 to the list

list.append(40)

list.append(50)

list.append(60)

print(list)

Output:

[10, 20, 30, 40, 50, 60]

 

§  append(List) – appends a new List to the end of the list.



list1=[10,20,30]

# Append another list

list2 = [8, 9]

list1.append(list2)

print(list1)

Output:

[10, 20, 30, [8, 9]]



list1=[10,20,30]

# Append an element of a different type, as list elements do not need to have the same type

list2 = [8, 9]

list1.append(list2)

list1.append('python')

print(list1)

Output:

[10, 20, 30, [8, 9], 'python']



Note :

§  the append() method only appends one new element to the end of the list. If you append a list to another list, the list that you append becomes a single element at the end of the first list.



extend(enumerable) – extends the list by appending elements from another enumerable

list1=[10,20,30]

# Extend list by appending all elements from b

list2 = [40, 50]

list1.extend(list2)

print(list1)

# Extend list with elements from a non-list enumerable:

list1.extend(range(3))

print(list1)

Output:

[10, 20, 30, 40, 50]

[10, 20, 30, 40, 50, 0, 1, 2]


§
 
Count length of list

my_list=[1,2,3,4,5,6]

print(len(my_list)) #  the output will be 6



insert() method in Python List

§  With the help of insert method, you can place an element on a specific index position.

§  This method takes two parameters.

index - position where an element needs to be inserted

element - this is the element to be inserted in the list

Return Value from insert()

§  It only inserts the element to the list It doesn't return anything; returns None.

 

list1=[10,20,30]

print(list1)#The Output [10, 20, 30]

list1.insert(3,40)

print(list1)#The Output [10, 20, 30, 40]

list1.insert(1,50)

print(list1)#The Output [10, 50, 20, 30, 40]

list1.insert(6,60)

print(list1)#The Output [10, 50, 20, 30, 40, 60]





remove() method in Python List



§  With the help of remove () method we can remove the first matching element (which is passed as an argument) from the list.

The syntax of the remove() method is:

list.remove(element)

remove() Parameters

§  The remove() method takes a single element as an argument and removes it from the list.

§  If the element doesn't exist, it throws ValueError: list.remove(x): x not in list exception.



Return Value from remove()

It only remove the element from the list It doesn't return anything; returns None

#cities list

cities=['agra','delhi','jaipur','kanpur','lucknow']

print('Original Cities list: ', cities)

# 'agra' is removed

cities.remove('agra')

# Updated Cities List

print('Updated Cities list: ', cities)



Output:

Original Cities list:  ['agra', 'delhi', 'jaipur', 'kanpur', 'lucknow']

Updated Cities list:  ['delhi', 'jaipur', 'kanpur', 'lucknow']









pop() method in Python List



§  With the help of pop () method we can remove the last item from the list and returns the removed item.

The syntax of the pop() method is:

list.pop()

Return Value from pop() method

§  pop() method with-out parameter returns the last item of the list

#cities list

cities=['agra','delhi','jaipur','kanpur','lucknow']

print('Original Cities list: ', cities)

# last item is removed

cities.pop()

# Updated Cities List

print('Updated Cities list: ', cities)

# last item is removed

cities.pop()

# Updated Cities List

print('Updated Cities list: ', cities)



Output:

Original Cities list: ['agra', 'delhi', 'jaipur', 'kanpur', 'lucknow']

Updated Cities list: ['agra', 'delhi', 'jaipur', 'kanpur']

Updated Cities list: ['agra', 'delhi', 'jaipur']



pop(index) method in Python List

§  With the help of pop () method we can remove the item at the given index from the list and returns the removed item.

The syntax of the pop() method is:

§  list.pop(index)

pop() Parameters

§  The remove () method takes a single element as an argument.

§  this  list.pop([i]) method to return the item which are present at the given specified  index position from the list and then remove that item from the list

§  If the element doesn't exist, it throws ValueError: list.remove(x): x not in list exception.



Return Value from pop() method

§  pop() method with parameter returns the item at specified  index position of the list



#cities list

cities=['agra','delhi','jaipur','kanpur','lucknow']

print('Original Cities list: ', cities)

#  remove and return the 2th item

deleted_city=cities.pop(1)

print(deleted_city,'is delete from cities list')

# Updated Cities List

print('Updated Cities list: ', cities)

#  remove and return the 11th item



Output

Original Cities list:  ['agra', 'delhi', 'jaipur', 'kanpur', 'lucknow']

delhi is delete from cities list

Updated Cities list:  ['agra', 'jaipur', 'kanpur', 'lucknow']



cities.pop(10)

# Updated Cities List

print('Updated Cities list: ', cities)

cities.pop(10)

IndexError: pop index out of range



§  index () method returns the index position of the element in the List.

§  If the same element is present more than once, the method returns the index of the first occurrence of the element.

§  If the element is not present then it will return Error: ValueError



#cities list

cities=['agra','delhi','jaipur','kanpur','lucknow']

pos=cities.index('delhi')

         print('delhi is present at index: ' ,pos)

Output       

delhi is present at index:  1





sort() method in Python List

§  With the help list.sort() method we can sort the elements of the  list in place.



The syntax of sort() method is:

list.sort(key=..., reverse=...)



§  Alternatively, you can also use Python's in-built function sorted() for the same purpose.



sorted(list, key=..., reverse=...)





§  The difference between sort() and sorted() method is only that sort() method doesn’t returns any value while, sorted() method returns an iterable list.



sort() Parameters



§  By default, sort () method doesn't take any extra parameters. However, it has two optional parameters that can be used to customize the operation



reverse

§  If true, the sorted list is reversed (or sorted in Descending order)



 key –

§  Specifies a function that is used to extract a comparison key from each list element. The default value is None (compares the elements directly).








#cities list

cities=['Jaipur','Delhi','Agra','kanpur','Lucknow']

print('Un-Sorting-cities: ',cities)

# sort the cities list

cities.sort()

print('Sorted cities',cities)



Output

Un-Sorting-cities:  ['Jaipur', 'Delhi', 'Agra', 'kanpur', 'Lucknow']

Sorted cities ['Agra', 'Delhi', 'Jaipur', 'Lucknow', 'kanpur']







Example 2: Sort a cities list in Descending Order

§  Setting reverse=True sorts the list in the descending order.

list.sort(reverse=True)

#cities list

cities=['Jaipur','Delhi','Agra','kanpur','Lucknow']

print('Un-Sorting-cities: ',cities)

# sort the cities list

cities.sort(reverse=True)

print('Sorted cities',cities)

Output

Un-Sorting-cities:  ['Jaipur', 'Delhi', 'Agra', 'kanpur', 'Lucknow']

Sorted cities ['kanpur', 'Lucknow', 'Jaipur', 'Delhi', 'Agra']



sorting a list by using your own function with key parameter?



Example 3: Sort a cities list by passing key function



cities.sort(key=len)

#cities list

cities=['Jaipur','Delhi','Agra','kanpur','Lucknow']

print('Un-Sorting-cities: ',cities)

# sort the cities list

cities.sort(key=len)

print('Sorted cities',cities)

Output

Un-Sorting-cities:  ['Jaipur', 'Delhi', 'Agra', 'kanpur', 'Lucknow']

Sorted cities ['Agra', 'Delhi', 'Jaipur', 'kanpur', 'Lucknow']

§  Here, no_digits is the Python's user-define function to count the no of digits of an element.

§  We want to sort this list in such a way that the number that have less number of digit come first.



# take element for sort

def no_digits(n):

    count=0

    while n>0:

        count+=1

        n=n//10

    return count

# number list

nums_list=[111,11111,1,1111,111111]

print('Un-Sorting-cities: ',nums_list)

# sort list with key

nums_list.sort(key=no_digits)

# print list

print('Sorted-List: ',nums_list)



Output

Un-Sorting-List:  [111, 11111, 1, 1111, 111111]

Sorted-List:  [1, 111, 1111, 11111, 111111]

www.CodeNirvana.in

Powered by Blogger.

About

Site Links

Popular Posts

Translate

Total Pageviews