0% found this document useful (0 votes)
13 views5 pages

List Python

Uploaded by

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

List Python

Uploaded by

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

List--

Container is an entity which contains multiple data items. It is also known as a collection or compound
data types.

Python has following data types

List Tuples

Sets Dictionaries

Lists commonly used for handling variable length data.

A list is defined by writing comma-seperated elements within[ ].

num = [10, 25, 35, 40, 100]

names = [ 'Sanjay', 'Anil', 'Narendra','Sourav' ]

ages = [23, 24, 23, 34, 35] #duplicates allowed

num = [10] * 5 # stores [10,10,10,10,10]

lst = [ ] # empity list, valid

##Like strings list can be sliced.

Looping in lists

If we wish to process each item in the list, we should be able to iterate through the list. This can done
using a while or for loop.

animals = [ ' Zebra', 'Tiger', 'Lion', 'Jackal','Kangaroo' ]

#using while loop

i=0

while i < len(animals):

print(animals[ i ])

i += 1

#using more covenient for loop

for a in animals :

print(a) SJD
While iterating through a list using a for loop, if we wish to keep track or index of the element that a is
referring to, we can do so using the bulit-in enumerate( ) function.

animals = [ ' Zebra', 'Tiger', 'Lion', 'Jackal','Kangaroo' ]

for index, a in enumerate (animals) :

print(index, a)

# unlike strings, lists are mutable.

Concatenation::::---

lst = [ 12, 15, 13, 23, 22, 16, 17 ]

lst = lst + [ 33, 44, 55 ]

print(lst) # prints [ 12, 15, 13, 23, 22, 23, 16, 17, 33, 44, 55]

Merging::::----

s= [10, 20, 30]

t=[100, 200, 300]

z= s + t

print(z) # prints [10, 20, 30, 100, 200, 300]

Conversion:::---

A string/ tuple / set can be converted into a list using the list( ) conversion function.

l = list('Africa') # converts the string to a list ['A', 'f', 'r', 'i', 'c', 'a']

Cloning::---

Rhis involves copying of one list to another. After copying both refer to different lists, though both
contain same values. Changing one list, doesnot change another. This operation is often known as deep
copy.

lst1 =[10, 20, 30, 40, 50]

lst2= [ ] #empty list

lst2 = lst2 + lst1 # lst1,lst2 refer to diffwrnt lists

print(lst1)

print(lst2) SJD
lst1[0]= 100

print(lst1[0],lst2[0])

Searching::----

An element can be searched in a list using the in memebership operator

lst = ['a', 'e', 'i', 'o', 'u']

res = 'a' in lst # returnTrue since 'a' is present in list

res = 'z' not in lst # return true since z is absent in list

Comparison::---

It is possible to compare contents of two lists. Comparison is done item by item till there is a missmatch.
In following code it would be decided that a is less than b when 3 and 5 are compared.

a = [1, 2, 3, 4]

b = [ 1, 2, 5]

print(a<b) #prints true

Using built in functions on lists

len(lst) # return number of items in the list

max(lst) # return maximum element in the list

min(lst) # return minimum element in the list

sum(lst) # return sum of all elements in the list

any(lst) # return true if any element of lst is true

all(lst) # return true if all element s of lst are true

del() # delete elements or sliced or entire list

sorted(lst) # return sorted list, lst remains unchanged

reversed(lst) # use for reversing lst SJD

List Methods::-----
Any list is an object of type list. Its method can be accessed using the syntax lst. method().

lst=[ 12, 15, 13, 23, 22, 22, 16, 17] # create list

lst.append(22) # at new item at end

lst.remove(13) # delete item 13 from the list

lst.pop( ) # removes last item in list

lst.pop(3) # remove 3rd item in the list

lst.insert(3,21) # insert 21 at 3rd position

lst.count(23) # return no of times 23 appears in lst

idx= lst.index(22) # return index of item 22

lst = [10, 2, 0, 50, 4]

lst.reverse( )

print(lst)

lst.sort( )

print(lst)

lst.sort(reverse= True)

print(lst)

Note that reverse( ) and sort( ) donot return a list. Both manipulate the list in place....

SJD

You might also like