List Method /Function
Method |
Description |
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
# list of wild animals
# appending wild_animals list to the animals list
print
output Updated
animals list: ['cat', 'dog', 'rabbit',
['tiger', 'fox']] |
|
Removes all the elements from
the list Example : >>>fruits
= ['apple', 'banana', 'cherry', 'orange'] >>>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'] |
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. |
|
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] |
|
Returns the index of the first
element with the specified value syntax : list.index(element,
start, end) list index() parameters
The list ·
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
# remove and return the 4th item
print # Updated List print
Output Return Value:
French Updated List:
['Python', 'Java', 'C++', 'C'] Example 2: pop() without an
index, and for negative indices
# programming languages list
# remove and return the last item print print print # remove and return the last item print print print # remove and return the third last item print print print
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
# 'rabbit' is removed
# Updated animals List print
Updated animals list: ['cat', 'dog', 'guinea pig']
Example 2: remove() method on a
list having duplicate elements
If a list contains duplicate elements, the # animals list
# 'dog' is removed
# Updated animals list print 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