Python List

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 15

List manipulation

Introduction
Why lists are Mutable?
Creating and Accessing List elements
Comparing List elements
List operators(+ , *)
List-Introduction
• The python lists are containers that are used to store a list of values of any
type.
• Lists are Mutable, i.e. We can change the elements of a list in place, python
will not create a fresh list when you make changes to an elements of a list.
• List is represented by square brackets[].
Examples:
• []-------------empty list
• [1,2,3]-------List of integers
• [‘a’,’b’,’c’]-----------List of characters
• [3,1.5,6,2.0.3.6]------------List of numbers(int and float)
• [‘Nikhil’,’neeraj’,’seshu’,’menu’]----------List of strings
• [‘a’,4,4.5,’python’]------------List of mixed value types
• [1,2,3[5,6,7]]---------------nested list
How Lists are mutable
>>> list=['c','c++','java','basic']
>>> id(list)
37656936list are mutable data type
>>> list[0]='c#‘change the value
>>> list
['c#', 'c++', 'java', 'basic']
>>> id(list)
37656936same value
>>> l=[11,22,33]
>>> list=['c','c++','java','basic']
>>> id(list)
37656936
>>> list[0]='c#'
>>> l=[11,22,33]
>>> type(l) if you want to know the type
<class 'list'>
>>> id(l)if you want to know the memory location it is according to my system it will vary
based on your system
9739240
>>>
>>> l[0]=77to change the value
>>> l
[77, 22, 33]
>>> id(l)  Now you can check the id it will give the same
9739240
>>> a=20 integers are immutable data type
>>> type(a)
<class 'int'>
>>> id(a)
269564464 memory address
>>> a=a+50
>>> a
70
>>> id(a)
269565264here  changed ie we are say that it is immutable
Creating lists
To create a list, put number of expressions in square brackets.
That is Use square brackets[] , to indicate the start and end of the list, and separate the
items by commas.

Accessing lists:
• The individual elements of a list are accessed through their indexes.
• To access the element of a list, we can either use Forward indexing or Backward
indexing.
• Like strings, if we give index out of the legal indices, while accessing individual elements
, python will raise index error
>>> List=['c','c++','java','basic']created the list
>>> type(List)to know the type
<class 'list'>--result
>>> List[3] to know the value+ve index
'basic‘ result
>>> List[-3]-ve index
'c++‘ result
>>> List[len(List)-1]length
'basic'
>>> List[6] no item
Result: error
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
List[6]
IndexError: list index out of range
Traversing a list:
• Traversal of a sequence means accessing and processing each element of it.
• For loop is very handy to access python sequences.
• Syntax:
• For<item>in<list>:
• process each item here

>>> List=['c','c++','java','basic']
>>> List
['c', 'c++', 'java', 'basic']
>>> for i in List:
print(i)

c
c++
java
basic

The loop variable [i] in the loop will assigned the list elements, one at a time
>>> a=[1,2,3]
>>> a
[1, 2, 3]
>>> for i in a:
print(i*2) here list value multiply by 2

Result:
2
4
6

Using range function


>>> a=[1,2,3]
>>> for i in range(len(a)):
print(a[i])

1
2
3
Program to print list of elements along with its index
a= ['p','y','t','h','o','n']
l=len(a)
for i in range(l):
print("At indexes",i,'and', i-l,':',a[i])
At indexes 0 and -6 : p
At indexes 1 and -5 : y
At indexes 2 and -4 : t
At indexes 3 and -3 : h
At indexes 4 and -2 : o
At indexes 5 and -1 : n

PYTHON  FORWARD INDEX P0,Y1,T2,H3,O4,N5


PYTHONBACKWARD INDEX P-6,Y-5,T-4,H-3,O-2,N-1
Comparing lists
• We can compare two lists using standard comparison operators i.e. Relational:<, <=,>,>=,==,!=

• >>> a=[1,2,3]
• >>> b=[1,2,3,4]
• >>> a>b
• False
• >>> b>a
• True
• >>> a=[1,2,3]
• >>> b=[1,2,3]
• >>> c=[3,4,5]
• >>> a==b
• True
• >>> a==c
• False
• >>> a>b
• False
• >>> c>a
• True
• >>>
• For lists,having characters, comparison done on the basis of ASCII values.[A=65..Z=90,a=97,z=122]
• >>> a=['a','b','c']
• >>> b=['A','B']
• >>> a>b
• True
• >>> a=['Anu']
• >>> b=['And']
• >>> a<b
• False
• >>> a=['AnU']
• >>> b=['And']
• >>> a<b
• True
• >>>
To compare the list, corresponding elements must be of comparable type.
It should be same type if not it will give error
>>> a=[1,2,3]int
>>> b=['a','b','c']character
>>> a<b
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
a<b
TypeError: '<' not supported between instances of 'int' and 'str'
>>>

List operations
Joining lists(+)
Replicating lists(*)
The concatenation operator(+), when used with two lists, joins two lists.
The + operator concatenates two lists and creates a new list.
>>> a=[1,2,3]
>>> b=[5,6,7]
>>> a
[1, 2, 3]
>>> b
[5, 6, 7]
>>> a+b
[1, 2, 3, 5, 6, 7]
>>> a=[1,2,3]
>>> b=['a','b']
>>> a+b
[1, 2, 3, 'a', 'b']
>>> b+a
['a', 'b', 1, 2, 3]
>>>
>>> c=a+b+[]  no changes
>>> c
[1, 2, 3, 'a', 'b']
>>> c=a+b+[[]]  list within in another list  it is possible
>>> c
[1, 2, 3, 'a', 'b', []]

>>> c=a+b+2 can only concatenate list not int


Traceback (most recent call last):
File "<pyshell#61>", line 1, in <module>
c=a+b+2 not possible to add an int
TypeError: can only concatenate list (not "int") to list

>>> c=a+b+[2] it is possible


>>> c
[1, 2, 3, 'a', 'b', 2]
>>>
The resultant list will have firstly elements of first list, followed by the elements of second list.
The + operator when used with lists requires that both the operands must be of list type.
Example:
List +List valid
List + integer invalid
String + list  invalid

>>> [1,2,3]+'python'
Traceback (most recent call last):
File "<pyshell#64>", line 1, in <module>
[1,2,3]+'python‘ list + string results in an error
TypeError: can only concatenate list (not "str") to list
>>> [1,2,3]+2
Traceback (most recent call last):
File "<pyshell#65>", line 1, in <module>
[1,2,3]+2 list + int results in an error
TypeError: can only concatenate list (not "int") to list
>>>
Repeating or replicating lists:
Like strings, we can use * operator to replicate a list specified number of times.
• Operator requires the operands to be of integer and list or list and integer.
• Example:
• List * integer  valid
• List * list  invalid
• String * list  invalid

• A list is replicated specified number of times


• >>> a=[1,2,3]
• >>> a*3
• [1, 2, 3, 1, 2, 3, 1, 2, 3]

• We cannot replicated a list with another list.


• >>> a*[15,10]
• Traceback (most recent call last):
• File "<pyshell#69>", line 1, in <module>
• a*[15,10]
• TypeError: can't multiply sequence by non-int of type 'list'
• >>>

You might also like