Python Tutorial: List Mathod/Function

Saturday, 8 August 2020

List Mathod/Function

List Method /Function

Method

Description

append()

Adds an element at the end of the list

Example 1: Adding Element to a List

# animals list

animals = ['cat', 'dog', 'rabbit']

# 'guinea pig' is appended to the animals list

animals.append('guinea pig')

# Updated animals list

print('Updated animals list: ', animals)

Output

Updated animals list:  ['cat', 'dog', 'rabbit', 'guinea pig']

Example 2: Adding List to a List

# animals list
animals = ['cat', 'dog', 'rabbit']
# list of wild animals
wild_animals = ['tiger', 'fox']
# appending wild_animals list to the animals list
animals.append(wild_animals)
print('Updated animals list: ', animals)

output

Updated animals list:  ['cat', 'dog', 'rabbit', ['tiger', 'fox']]

clear()

Removes all the elements from the list

Example :

>>>fruits = ['apple''banana''cherry''orange']
>>>fruits.clear()

>>>print(fruits)

[]

copy()

Returns a copy of the list.

For Example

>>> Name=["Ankita", "Vanshika", "Shikha","Anshu","Aviral","Anchit"]

>>> print(Name) #display list

['Ankita', 'Vanshika', 'Shikha', 'Anshu', 'Aviral', 'Anchit']

>>> sName=Name.copy() #copying list

>>> print("Copied List :",sName)

Output

Copied List : ['Ankita', 'Vanshika', 'Shikha', 'Anshu', 'Aviral', 'Anchit']

>>> sName1=Name[2::] #copying slice list

>>> print(sName1)

 Output

['Shikha', 'Anshu', 'Aviral', 'Anchit']

>>> sName2=Name[::3] #copying slice list

>>> print("Copied Slice :",sName2)

Output

Copied Slice : ['Ankita', 'Anshu']

count()

Returns the number of elements with the specified value

For Example

>>> Name=["Ankita", "Vanshika", "Shikha","Anshu","Aviral","Anchit","Shikha"]

>>> print("Name Shikha ",Name.count("Shikha"), "times.")

Output

Name Shikha  2 times.

extend()

Add the elements of a list (or any iterable), to the end of the current list

For Example

>>> Age=[43,65,23,43,76]

>>> youngAge=[8,13,15]

>>> Age.extend(youngAge)

>>> print(Age)

Output

[43, 65, 23, 43, 76, 8, 13, 15]

index()

Returns the index of the first element with the specified value

syntax :

list.index(element, start, end)

list index() parameters

The list index() method can take a maximum of three arguments:

·         element - the element to be searched

·         start (optional) - start searching from this index

·         end (optional) - search the element up to this index

·         For Example :

·         >>> Name=["Ankita", "Vanshika", "Shikha","Anshu","Aviral","Anchit","Shikha"]

·         >>> print("The Index of Aviral :",Name.index("Aviral"))

·         Output

·         The Index of Aviral : 4

insert()

Adds an element at the specified position

Syntax:

list.insert(i, elem)

Where :

·         index - the index where the element needs to be inserted

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

Notes:

·         If index is 0, the element is inserted at the beginning of the list.

·         If index is 3, the element is inserted after the 3rd element. Its position will be 4th

For example :

>>> Age=[43,65,23,43,76]

>>> Age.insert(3,99)

>>> print("Updated List :",Age)

Output

Updated List : [43, 65, 23, 99, 43, 76]

pop()

Removes the element at the specified position

Example 1: Pop item at the given index from the list

# programming languages list
languages = ['Python', 'Java', 'C++', 'French', 'C']
# remove and return the 4th item
return_value = languages.pop(3)
print('Return Value:', return_value)
# Updated List
print('Updated List:', languages)

Output

Return Value: French

Updated List: ['Python', 'Java', 'C++', 'C']

Example 2: pop() without an index, and for negative indices

# programming languages list
languages = ['Python', 'Java', 'C++', 'Ruby', 'C']
# remove and return the last item
print('When index is not passed:') 
print('Return Value:', languages.pop())
print('Updated List:', languages)
# remove and return the last item
print('\nWhen -1 is passed:') 
print('Return Value:', languages.pop(-1))
print('Updated List:', languages)
# remove and return the third last item
print('\nWhen -3 is passed:') 
print('Return Value:', languages.pop(-3))
print('Updated List:', languages)

Output

When index is not passed:

Return Value: C

Updated List: ['Python', 'Java', 'C++', 'Ruby']

When -1 is passed:

Return Value: Ruby

Updated List: ['Python', 'Java', 'C++']

When -3 is passed:

Return Value: Python

Updated List: ['Java', 'C++']

remove()

Removes the first item with the specified value

For Example

Example 1: Remove element from the list

# animals list
animals = ['cat', 'dog', 'rabbit', 'guinea pig']
# 'rabbit' is removed
animals.remove('rabbit')
# Updated animals List
print('Updated animals list: ', animals)
Output :
Updated animals list:  ['cat', 'dog', 'guinea pig']
 

Example 2: remove() method on a list having duplicate elements

If a list contains duplicate elements, the remove() method only removes the first matching element.

# animals list
animals = ['cat', 'dog', 'dog', 'guinea pig', 'dog']
# 'dog' is removed
animals.remove('dog')
# Updated animals list
print('Updated animals list: ', animals)
Output :
Updated animals list:  ['cat', 'dog', 'guinea pig', 'dog']

reverse()

Reverses the order of the list

For Example : Reverse a List

>>> languages = ['Python', 'Java', 'C++', 'Ruby', 'C']

>>> languages.reverse()

>>> languages

['C', 'Ruby', 'C++', 'Java', 'Python']

For Example : Reverse a List Using Slicing Operator

# Operating System List

systems = ['Windows', 'macOS', 'Linux']

print('Original List:', systems)

# Reversing a list  

#Syntax: reversed_list = systems[start:stop:step]

reversed_list = systems[::-1]

# updated list

print('Updated List:', reversed_list)

Original List: ['Windows', 'macOS', 'Linux']

Updated List: ['Linux', 'macOS', 'Windows']

sort()

Sorts the list

For Example

>>> languages = ['Python', 'Java', 'C++', 'Ruby', 'C']

>>> languages

Output

['C', 'C++', 'Java', 'Python', 'Ruby']

For example

>>> Score=[43,45,98,23,56,78,90,14]

>>> Score.sort()

>>> Score

Output

[14, 23, 43, 45, 56, 78, 90, 98]

 


No comments:

Post a Comment