Sunday, February 2, 2020

List Manipulations - Methods


List have many different methods . Some of that are available with list object in Python programming are:



index() 
In simple terms, the index() method finds the given element in a list and returns its position. If the same element is present more than once, the method returns the index of the first occurrence of the element.

The syntax of the index() method is:
list.index(element)


Example
p = ['p', 'r', 'o', 'g', 'r', 'a', 'm']
index = p.index('o')
print('The index of o:', index)
index = p.index('r')
print('The index of r:', index)

Output
The index of o: 2
The index of r: 1
---------------------------------------------------------------------------------------------------------------


append()
To add an item to the end of the list, use the append() method.
The syntax of the append() method is:
list.append(item)

Example
list = ["aaa", "bbb", "ccc"]
list.append("ooo")
print(list)

Output
['aaa', 'bbb', 'ccc', 'ooo']
------------------------------------------------------------------------------------------------------------------


insert() 

The insert() method inserts an element to the list at a given index.

The syntax of insert() method is
list.insert(index, element)

Example
list = ['aaa', 'bbb', 'ccc']

list.insert(1, "ooo")
print(list)

Output
['aaa', 'ooo', 'bbb', 'ccc']
-------------------------------------------------------------------------------------------------------------------

copy()

The copy() method returns a shallow copy of the list. It gives shallow copy that means modification in new list will not affect old list

The syntax of copy() method is:
new_list = list.copy()

Example
l = ['aaa', 'bbb', 'ccc', 'ooo']

x = l.copy()
print(l)

Output
['aaa', 'bbb', 'ccc', 'ooo']
-------------------------------------------------------------------------------------------------------------------------

extend()
The extend() method adds the specified list elements (or any iterable) to the end of the current list.

The syntax for extend() method
list.extend(seq)

Example
b1 = ['cse', 'ece', 'eee']
b2 = ['mec', 'civ', 'aero']
b1.extend(b2)
print(b1)

print(b2)

Output
['cse', 'ece', 'eee', 'mec', 'civ', 'aero']

['mec', 'civ', 'aero']
----------------------------------------------------------------------------------------------------------------------

count()
Python list method count() returns count of how many times obj occurs in list.

Syntax
The syntax for count() method
list.count(obj)

Example
L = [123, 'apple', 'banana', 'orange', 123];
print( "Count for 123 : ", L.count(123))
print ("Count for banana : ", L.count('banana'))

Output
Count for 123 :  2
Count for banana :  1
--------------------------------------------------------------------------------------------------------------------

remove()

The remove() method removes the first occurrence of the element with the specified value.

Syntax
list.remove(elmnt)

Example
b = ['cse', 'ece', 'eee']
b.remove("ece")
print(b)

Output
['cse', 'eee']
------------------------------------------------------------------------------------------------------------------

pop()

The pop() method removes the element at the specified position.

Syntax
list.pop(pos)

Example
languages = ['Python', 'Java', 'C++', 'Dotnet', 'C']
return_value = languages.pop(3)
print('Return Value:', return_value)
print('Updated List:', languages)


Output
Return Value: Dotnet
Updated List: ['Python', 'Java', 'C++', 'C']
----------------------------------------------------------------------------------------------------------------

reverse()
The reverse() method reverses the sorting order of the elements.

Syntax
list.reverse()

Example
b = ['cse', 'ece', 'eee','it','aero']
print('Original List:', b)
b.reverse()
print('Updated List:', b)

Output

Original List: ['cse', 'ece', 'eee', 'it', 'aero']
Updated List: ['aero', 'it', 'eee', 'ece', 'cse']
---------------------------------------------------------------------------------------------------------------

sort()

The sort() method sorts the elements of a given list in a specific order - Ascending.

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=...)

Example
b = ['cse', 'ece', 'eee','it','aero']
b.sort()
print('Sorted List:', b)

Output

Sorted List: ['aero', 'cse', 'ece', 'eee', 'it']
----------------------------------------------------------------------------------------------------------------------- 

len() 

Python list function len() returns the number of elements in the list.

Syntax
The syntax for len() method
len(list)

Example
b = ['cse', 'ece', 'eee','it','aero']
c = ['red','green','blue']
print( "First list length : ", len(b))
print ("Second list length : ", len(c))

Output
First list length :  5
Second list length :  3
----------------------------------------------------------------------------------------------------------------------

Nested List
A list can contain any sort object, even another list (sublist), which in turn can contain sublists themselves, and so on. This is known as nested list. A nested list is created by placing a comma-separated sequence of sublists.

Example1
L = ['a', ['bb', ['ccc', 'ddd'], 'ee', 'ff'], 'g', 'h']



Example2
b = ['cse', 'ece', ['eee','it',['aero','mech']],'civ','h&s']
print(b[2])   
print(b[2][2]) 
print(b[2][2][0])

Output
['eee', 'it', ['aero', 'mech']]
['aero', 'mech']
aero
-------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment