0% found this document useful (0 votes)
6 views3 pages

List

Uploaded by

yashkshatriya108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

List

Uploaded by

yashkshatriya108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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.

>>> squares = [1, 4, 9, 16, 25]


>>> squares
[1, 4, 9, 16, 25]
>>> squares[0]
1

Methods on lists
>>> fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi',
'apple']

 Append - Add an item to the end of the list

>>> 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

 Count - Return the number of times x appears in the list

>>> fruits.count('apple')
2

 Sort - Sort the items of the list in place

>>> fruits.sort()
>>> fruits
['apple', 'apple', 'banana', 'banana', 'grape', 'kiwi',
'orange', 'pear']

 Reverse - Reverse the elements of the list in place.

>>> fruits.reverse()
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple',
'orange']

 Copy - Return a shallow copy of the list.

>>> fruits.append('grape')
>>> fruits
['banana', 'apple', 'kiwi', 'banana', 'pear', 'apple',
'orange', 'grape']

You might also like