Chapter - 7 Python List Manipulations - 0
Chapter - 7 Python List Manipulations - 0
>>> List=['C','o','m','p','u','t','e','r']
>>> print(len(List))
8
>>> str="Computer"
>>> print(len(str))
output:
8
SMILILARITIES WITH STRING
Smililarities with string.
3. Indexing:
List[i] will return the value at index i of the List. The first item
has index 0.
Example
>>> print(List[1])
o #output it is 2nd element of list whose index is 1
>>> print(str[1])
o #output it is 2nd element of string whose index is 1
>>> print(List[-1])
r #output it is last element of list whose index is 1
>>> print(str[-1])
r #output it is last element of string whose index is 1
SMILILARITIES WITH STRING
Smililarities with string.
4. Slicing:
List[i:j] will return a new list, containing objects at indexes between i and j
(including i but excluding j index)
Example
>>> print(List[0:5])
compu #output it is 2nd element of list whose index is 1
>>> print(str[0:5])
compu #output it is 2nd element of string whose index is 1
>>> print(List[-1:])
r #output it is last element of list whose index is 1
>>> print(str[-1])
r #output it is last element of string whose index is 1
SMILILARITIES WITH STRING.
Smililarities with string.
5. Concatenation and Replication Operators + and *:-
The + operator adds one list to the end of second list.
>>>List1=[10,20,30]
>>>List2=[40,50,60] Replication of List using * operator
>>>List3=List1+List2
>>>print(List1)
[10, 20, 30]
>>> vowels=['a','e','i','o','u']
>>>Print(List2)
[40, 50, 60] >>> vowels*2
>>>Print(List3)
[10, 20, 30, 40, 50, 60]
['a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', 'u']
SMILILARITIES WITH STRING
Smililarities with string. >>> 'r' not in str
5. Membership Operators (in and not in): Output
>>>Str=”Computer” False
>>> List1=['C','o','m','p','u','t','e','r']
>>> 'r' in str
>>> 's‘ not in str
Output Output
True true
>>> 's' in str
Output
>>> 'r' not in List
False Output
>>> 'r' in List False
Output
True
>>> 's' not in List
>>> 's' in List Output
Output True
False
DIFFERENCE FROM STRING
Difference in list and String
Lists are mutable but string is immutable.
>>> vowels=['a','e','i','o','u']
>>> str="aeiou“
>>> vowels[4]='y‘ #changing list element in place
>>> vowels
['a', 'e', 'i', 'o', 'y'] #look changed list value.
>>> list=[10,20]
>>> list*2
[10, 20, 10, 20]
>>> list*2.5
Traceback (most recent call last):
File "<pyshell#118>", line 1, in <module>
list*2.5
TypeError: can't multiply sequence by non-int of type 'float'
LIST OPERATIONS
3. Slicing the Lists: List slices are like string slices and are the subpart
of a list extracted out. We can use indexes of list elements to
create list slices as per following format
seq=L[start:stop]
>>> Lst=[10, 12, 14, 20, 22, 24, 30, 32, 34]
>>> seq=Lst[3:-3] #slicing index 3 not included
>>> seq
[20, 22, 24]
>>> seq[1]=28
>>> seq
[20, 28, 24]
>>> Lst[3:30] #since there are not 30 indexes hence wil extract elemenst starting from index 3 to the end of the list
[20, 22, 24, 30, 32, 34]
LIST OPERATIONS
3. Slicing the Lists:
>>> Lst[-17:7] #Starting index is very low but Python will start form -15 and will extract element onward < 7
[10, 12, 14, 20, 22, 24, 30]
>>>Lst[10:20]
[] #since no element falls in between given indexes
Note: L[Start:Stop] creates a list slice with elements falling between Start
and Stop indexes (Stop index not included) skipping step 1 elements in
between
>>>Lst[Start:Stop:Skip]
Example
>>>Lst[0:10:2]
[10, 14 , 22, 30, 34] #Look 2 alternate elements are extracted as skip is 2
>>>Lst[::3] #Start and Stop not given ony skip is given hence it will pick every 3rd element from the list
[10, 20, 30]
LIST OPERATIONS
3. Slicing the Lists:
Seq1=Lst[::2] # Seq1 will have every second item of the list
Seq2=Lst[5::2] #Seq2 will have every second element starting from
# index 5 i.e. sixth element
>>>Lst[::-1] # Will reverse the list
WORKING WITH LISTS
1. Appending Element to a List:
append() function is used to append item to the list. Its general
syntax is
List.append(item)
>>>List1=[10,20]
>>>List1.append(30)
>>>List1
[10, 20, 30]
WORKING WITH LISTS
2. Updating Element to a List:
To update or change an element of List in place we just have to
assign new value to the element’s index in the list as per syntax given
below
List[index]=<new value>