List

  1. Lists are one of the most powerful tools in Python.

  2. They are just like the arrays declared in other languages.

  3. But the most powerful thing is that list need not be always homogeneous.

  4. A single list can contain strings, integers, as well as objects.

  5. Lists can also be used for implementing stacks and queues.

  6. Lists are mutable, i.e., they can be altered once declared.

Example - 1:

# Declaring and printing list 
L = [1, "a" , "string" , 1+2] 
print(L)   

[1, 'a', 'string', 3]

Example - 2:

l = [ 'hello','hi','how are you' ] # Homogenous List(contains element of same type)
    #  0       1      2    (indexing)
print(l)
print(l[2]) #print element at index 2
l = [ 56,23,45,12,67,12,43,1,6,8,6,33,12] #Homogenous list
print("Homogeneous List :",l)
print(l[5])
l = [ 'hello','hi', 3, 4.5 ] #Non-homogeneous List (contains element of differnt type)
#       -4   -3   -2     -1
print("Non-Homogeneous List : ",l)
print(l[-3])

['hello', 'hi', 'how are you']

how are you

Homogeneous List : [56, 23, 45, 12, 67, 12, 43, 1, 6, 8, 6, 33, 12]

12

Non-Homogeneous List : ['hello', 'hi', 3, 4.5]

hi

Example - 3:

l = [ 56,23,45,12,67,12,43,1,6,8,6,33,12]
print(l[2:10])  #SLICING (print element from index 0 to n-1) here it prints element from index 2 to 9
print("Items in original list : ",len(l))
x = None 
y = None
z = -1
print("Reverse of List ",l[x:y:z])  #([starting_point:ending_point:step]) here starting and ending point is none and step is -1
x = -2
y = -9
z = -1
print(l[x:y:z])

[45, 12, 67, 12, 43, 1, 6, 8]

Items in original list : 13

Reverse of List [12, 33, 6, 8, 6, 1, 43, 12, 67, 12, 45, 23, 56]

[33, 6, 8, 6, 1, 43, 12]

Operation on a list to Add items

  1. append

  2. insert

  3. extend

To Delete items

  1. pop

  2. remove

To Common Operation

  1. sort

  2. copy

  3. reverse

  4. count

  5. index

  6. clear

Build_in Functions to Add Items

Example - 5:

#append(item) insert element at the end of the list
l = [ 'hi',3.14,'how']
l.append('item')
print(l)
l.append(122335)
print(l)
k = [ 9,8,7,6,5,4,3,]
l.append(k)
print(l)

['hi', 3.14, 'how', 'item']

['hi', 3.14, 'how', 'item', 122335]

['hi', 3.14, 'how', 'item', 122335, [9, 8, 7, 6, 5, 4, 3]]

Example - 6:

#insert(index,item) insert item at given index 
l = [ 'hello', 3.14,1452,432423,434,234]
l.insert(2,'hi')
print(l)

['hello', 3.14, 'hi', 1452, 432423, 434, 234]

Example - 7:

#extend(sequence) 
l = []
l.extend('python')
print(l)

['p', 'y', 't', 'h', 'o', 'n']

Example - 8:

l = ['ghc']
k = [ 'java','c','c++','python'] 
l.extend(k)
print(l)

['java', 'c', 'c++', 'python']

Build_in Functions to Remove Items

Example - 9:

#delete item from list
l = [ 5,6,67,5324,324]
print(l)
x = l.pop() #just opposite of append i.e delete the last element in list
print("Item ",x,"is deleted from list")
print(l)

[5, 6, 67, 5324, 324]

Item 324 is deleted from list

[5, 6, 67, 5324]

Example - 10:

#pop(index)
l = [10,9,9,34,2,43213,21]
print(l)
k = l.pop(5)
print("item at index 5 is {} is deleted".format(k))
print(l)

[10, 9, 9, 34, 2, 43213, 21]

item at index 5 is 43213 is deleted

[10, 9, 9, 34, 2, 21]

Example - 11:

#remove(item) remove particular item from list
l = [ 'hi','hello','how are you','hi','hi']
print(l)
l.remove('hi')
print(l)

['hi', 'hello', 'how are you', 'hi', 'hi']

['hello', 'how are you', 'hi', 'hi']

Practice Problems:

Example - 12:

#Common Function on Python List
#sort()-> it will sort list in accending ( list should be homogenous )
#sort(reverse=True)->it will sort in decending order
l = [ 56,23,45,12,67,12,43,1,6,8,6,33,12]
k = l #k uses the same address of l
print(k)
k.sort() #sort the list k
print("Sorted k : ",k)
print(l)
l.sort(reverse=True)
print("Sorted l : ",l)

[56, 23, 45, 12, 67, 12, 43, 1, 6, 8, 6, 33, 12]

Sorted k : [1, 6, 6, 8, 12, 12, 12, 23, 33, 43, 45, 56, 67]

[1, 6, 6, 8, 12, 12, 12, 23, 33, 43, 45, 56, 67]

Sorted l : [67, 56, 45, 43, 33, 23, 12, 12, 12, 8, 6, 6, 1]

Example - 13:

l = [ 'zebra','apple','something','eye','flag','computer','python']
k = [ 'zebra','apple','something','eye','flag','computer','python']
print(*l)  #prints only elements in list l 
l.sort()
print(*l)  #print after sorting alphabetically
print(*k)
k.sort(reverse=True)
print(*k)

zebra apple something eye flag computer python

apple computer eye flag python something zebra

zebra apple something eye flag computer python

zebra something python flag eye computer apple

Example - 14:

mylist = [ 
    ['hi',10,32],
    ['abc',100,2,3,],
    ['something',5,6,7,8],
    ['popcorn',9,10,11,12]
]
print(mylist)
mylist.sort() #sort according to the element at index 0 of every sublist
print(mylist)

[['hi', 10, 32], ['abc', 100, 2, 3], ['something', 5, 6, 7, 8], ['popcorn', 9, 10, 11, 12]]

[['abc', 100, 2, 3], ['hi', 10, 32], ['popcorn', 9, 10, 11, 12], ['something', 5, 6, 7, 8]]

Example - 15:

#reverse -> Reverse the order of items in the list
l = [ 1,2,3,4,5,6,7]
x = l[::-1] #reverse the list through slicing
print(x)
print(l)
l.reverse()
print(l)

[7, 6, 5, 4, 3, 2, 1]

[1, 2, 3, 4, 5, 6, 7]

[7, 6, 5, 4, 3, 2, 1]

Example - 16:

#copy -> Returns a shallow copy of the list
l = [ 1,2,3,4,5]
k = l.copy()
z = l
print(l)
l[2]= 5 #will change only in list l not in their copies
print(k)
print(z)
print(l)

[1, 2, 3, 4, 5]

[1, 2, 3, 4, 5]

[1, 2, 5, 4, 5]

[1, 2, 5, 4, 5]

Example - 17:

#index -> Returns the index of the first matched item
l = [ 12,3,4532,3213,45,423,453241,53,42,4542,4,63243,4542,5,34,3445,3,34,34,234,324]
k = l.index(4542)
print(k)
print(l[k+2])
l = [ 'hello','hi',12,23,23,[1,23],'hi']
k = l.index('hi')
j = l.index([1,23])
print(k)
print(j)

9

63243

1

5

Example - 18:

#clear -> Removes all items from the list
mylist = [ 'hello','hi','bye',1,2,3]
print(mylist)
mylist.clear()
print(mylist)

['hello', 'hi', 'bye', 1, 2, 3]

[]

Example - 19:

#count -> Returns the count of number of items passed as an argument
l = [ 1,6,3,2,1,34,64,1,3,5654,1,344,62,1,346,3,1,35464,1,35235,1,4352,3]
print(l)
x = l.count(3)
y = l.count(1)
print(x)
print(y)

[1, 6, 3, 2, 1, 34, 64, 1, 3, 5654, 1, 344, 62, 1, 346, 3, 1, 35464, 1, 35235, 1, 4352, 3]

4

8

Last updated