List
List
Study Notes
List
Data Analytics
List
Lists
A list is a data structure in python which is ordered and mutable i.e. it is possible to change its
content.
List allows duplicate members as well.
List always starts with index as 0.
List supports operations like concatenation.
Methods on lists
>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi',
'apple']
>>> fruits.append('banana')
['orange', 'apple', 'pear', 'banana', 'kiwi',
'apple',’banana’]
Extend - Extend the list by appending all the items from the iterable.
Insert - Insert an item at a given position.
Len - Returns length of the list or size of the list.
Max - return maximum element of given list.
Min - return minimum element of given list.
Remove - Remove the first item from the list whose value is equal to x.
Pop - Remove the item at the given position in the list, and return it.
>>> fruits.pop()
'banana'
List
Clear - Remove all items from the list
Index - Return zero-based index in the list of the first item whose value is equal to x.
>>> fruits.index('banana')
3
>>> fruits.count('apple')
2
>>> fruits.sort()
>>> fruits
['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi',
'orange', 'pear']
>>> fruits.reverse()
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple',
'orange']
>>> fruits.append('grape')
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple',
'orange', 'grape']