List
Lists are one of the most powerful tools in Python.
They are just like the arrays declared in other languages.
But the most powerful thing is that list need not be always homogeneous.
A single list can contain strings, integers, as well as objects.
Lists can also be used for implementing stacks and queues.
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:
[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
append
insert
extend
To Delete items
pop
remove
To Common Operation
sort
copy
reverse
count
index
clear
Build_in Functions to Add Items
Example - 5:
['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:
['hello', 3.14, 'hi', 1452, 432423, 434, 234]
Example - 7:
['p', 'y', 't', 'h', 'o', 'n']
Example - 8:
['java', 'c', 'c++', 'python']
Build_in Functions to Remove Items
Example - 9:
[5, 6, 67, 5324, 324]
Item 324 is deleted from list
[5, 6, 67, 5324]
Example - 10:
[10, 9, 9, 34, 2, 43213, 21]
item at index 5 is 43213 is deleted
[10, 9, 9, 34, 2, 21]
Example - 11:
['hi', 'hello', 'how are you', 'hi', 'hi']
['hello', 'how are you', 'hi', 'hi']
Practice Problems:
Example - 12:
[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:
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:
[['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:
[7, 6, 5, 4, 3, 2, 1]
[1, 2, 3, 4, 5, 6, 7]
[7, 6, 5, 4, 3, 2, 1]
Example - 16:
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]
[1, 2, 5, 4, 5]
[1, 2, 5, 4, 5]
Example - 17:
9
63243
1
5
Example - 18:
['hello', 'hi', 'bye', 1, 2, 3]
[]
Example - 19:
[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
Was this helpful?