List
List
List
Ordered
When we say that lists are ordered, it means that the items have a defined order, and
that order will not change.
If you add new items to a list, the new items will be placed at the end of the list.
Changeable
The list is changeable, meaning that we can change, add, and remove items in a list after
it has been created.
Allow Duplicates
Since lists are indexed, lists can have items with the same value:
thislist = ["apple", "banana", "cherry", "apple", "cherry"]
print(thislist)
List Length
To determine how many items a list has, use the len() function:
hislist = ["apple", "banana", "cherry"]
print(len(thislist))
type()
lists are defined as objects with the data type 'list' : <class 'list'>
mylist = ["apple", "banana", "cherry"]
print(type(mylist))
<class 'list'>
List = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print("Original List:\n", List)
print("\nSliced Lists: ")
print(List[3:9:2])
print(List[::2])
print(List[::])
Original List:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Sliced Lists:
[4, 6, 8]
[1, 3, 5, 7, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
List Modification
L=[“one” , “two” ,”Three” ]
L[0:2]=[0,1]
L
[0,1,”Three”]
L[2]=3
L
[0,1,3]
List takes more memory than string and time for processing.
FUNCTION OF LIST
1. len() Length of a List
l1=[1,2,3,4,5,6]
ll=len(l1)
print(ll)
6
List1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
List2 = [11, 12, 13, 14, 15, 16, 17, 18,
19]
print(List1 + List2)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14,
15, 16, 17, 18, 19]
List1 = [1, 2, 3, 4, 5, 6]
print(List1*3)
[1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 1, 2, 3,
4, 5, 6]
l1=[1,2,3,4,5,6,7]
l1.append(8)
l1
[1, 2, 3, 4, 5, 6, 7, 8]
6. del Its not a method, it’s a statement
del(l1[1:4])
l1
[1, 5, 6, 7, 8]
7. Index() The index() method returns the index of
the specified element in the list.
l1
[1, 5, 6, 7, 8]
l1.index(7)
3
8. Extend() The extend() method adds all the items of
the specified iterable, such as list, tuple,
dictionary, or string , to the end of a list.
t1=[100,200,300]
l1.extend(t1)
l1
[1, 5, 6, 7, 8, 100, 200, 300]
9. Insert(pos,<item>) The insert() method inserts an element to
the list at the specified index.
l1.insert(3,500)
l1
[1, 5, 6, 500, 7, 8, 100, 200, 300]
10. Pop() The list pop() method removes the item at
the specified index. The method also
returns the removed item.
l1
[1, 5, 6, 500, 7, 8, 100, 200, 300]
l1.pop()
300
l1
[1, 5, 6, 500, 7, 8, 100, 200]
l1.pop(3)
500
l1
[1, 5, 6, 7, 8, 100, 200]
11. Remove() The remove() method removes the first
matching element (which is passed as an
argument) from the list.
t2=[1,2,3,4,4,5,4,6,4,8,4]
t2.remove(4)
t2
[1, 2, 3, 4, 5, 4, 6, 4, 8, 4]
12. Clear() The clear() method removes all items
from the list.
t2.clear()
t2
[]
13. Count() The count() method returns the number of
times the specified element appears in the
list.
t2=[2,3,4,4,5,4,6,7]
t2.count(4)
3
14. Reverse() The reverse() method reverses the
elements of the list.
t2.reverse()
t2
[7, 6, 4, 5, 4, 4, 3, 2]
15. Sort() The list's sort() method sorts the elements
of a list always in ascending order, to get
descending order use reverse method.
t2
[7, 6, 4, 5, 4, 4, 3, 2]
t2.sort()
t2
[2, 3, 4, 4, 4, 5, 6, 7]
t2.sort(reverse=True)
t2
[7, 6, 5, 4, 4, 4, 3, 2]