Lists in Python 7
List:- List is standard data type of Python Operator = is used to check equality of
that can a sequence of values belonging two lists.
to any type. a=[1,2,7,8]
Ex: L=[1,2,7,3] , L1=[1,3.5,’Hello’] b=[5,6,7]
List is an indexed data type and both print(a==b)
forward indexing (starting from 0) and output: False
backward indexing (from -1 ) is applicable. we can also used to assign elements of a
list to another list by using operator =.
Accessing Lists:We can access List by x=b
using membership operator in either Now b is another name a .
directly or by using index numbers.
A=[1,3,5,7] Similarity between list and string:
1: 2:By using index no. We can apply membership operator
for x in A: for x in range(len(A)): in to access individual elements of
print(x) print(A[x]) both data types.
output: Both are indexing data types as well
1 as we can perform slicing on both
3 data types.
5 Concatenation operator + and
7 replication operator * can be applied
List Operators: We can apply operators on both data types.
+, * ,in and = on any List. Difference between list and string:
Both are stored in same manner but
Operator + is used to concatenate two elements of list may vary in size but
lists. in strings each element will be of
a1=[1,2,7,8] same length (single character).
a2=[5,6,7] Lists are mutable data types on the
a=a1+a2 other hand strings are immutable.
print(a) Slicing The Lists:
output: [1,2,7,8,5,6,7] List slicing refers to a part of List
containing some contiguous Elements
Operator * is used to replicate any list.
from a List. following are some examples
A=[1,2,3]
of List slicing :
print(A*3)
1:
Output:
List[start:Stop]
[1,2,3,1,2,3,1,2,3]
Ex: a=[1,2,3,4,5,6,7,8,9,10]
Operator in is used to access individual print(a[3:7])
elements of any list. Output:[4,5,6,7]
for x in A: 2:
print(x) List[start:Stop:step]
output: Ex:
1 a=[1,2,3,4,5,6,7,8,9,10]
3 print(a[3:7:2])
5 Output: [4,6]
3: print(a) #Will print [4,5,7,8]
List[ :Stop] 5. pop():- pop method is used delete
Ex: a=[1,2,3,4,5,6,7,8,9,10] any element whose index is passed.
If no indexis passed last element of
print(a[:5])
list is deleted.
Output: [1,2,3,4,5] a = [4,7,8]
4: x=[Link](1)
List[:] print(x) #Will print 7
Ex: 6. remove():-This method removes
Ex: a=[1,2,3,4,5,6,7,8,9,10] the first occurrence of the element
print(a[:]) with the specified [Link]
function will report an error if
Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
element is not present in list.
5: a = [4,7,8]
String[ : :-1] [Link](7)
Ex: print(a) #Will print [4,8]
Ex: a=[1,2,3,4,5,6,7,8,9,10] 7. clear():-clear method is used to
print(a[:]) clear all the elements of a list.
Output: [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] a = [4,7,8]
[Link]()
print(a) #Will print []
:List Functions: 8. count():-This function is used to
1. index():- count occurrence of any element in
Will return the lowest index of a list.
element passed. a = [4,7,8,4]
start (Optional) – The position from c=[Link](4)
where the search begins. print(c) # will print 2.
end (Optional) – The position from 9. reverse():- This method is used to
where the search ends. reverse contents of an list.
This function results into an error If given a = [5,6,7,8]
element is not present is list. [Link]()
a = [1, 2, 3, 4, 1, 1, 1, 4, 5] print(a) # Will print [8,7,6,5]
# Will print index of '4' in sublist 10. sort():-This method is used to
# to print index of 4 from 4 to 8. arrange elements of a list in
print([Link](4, 4, 8)) ascending or descending [Link]
2. append():-append method of list is default it arrange elements in
used to add a new element at the ascending order.
end of list.
A=[1,2,3] a = [5,7,8,4]
[Link](4) will add a new element [Link]()
4 in list. print(a) # Will print [4,5,7,8]
3. extend():- Extend method is ued to [Link](reverse=True)
add multiple elements at the end of print(a) # Will print [8,7,5,4]
list. A=[1,2,3]
[Link]([4,5,6]) will add all
elements in list.
4. insert():-insert method is used to
insert any element at given
Computer Science
position . By
a = [4,7,8] ASHWANI SIR
[Link](1,5)
(Mob: 9319021577)