What
-
For storage of collection of objects. Can store different data types.
Creating List
a = [] //1D list
listofzeros = [0] * n //1D list of zeros of size=n
a = [[1, 2], //2-D List.Every row can different no of coloumns. `a[0][2]:Index error`
['test', 4, 50.2],
['play', 1990, 8, 9]
]
a = [ //3-D List. Packing 2 or more Matrices.
[
[1,2,3],
[4,5,6],
],
[
[7,8,9],
[10,11,12],
]
]
a = [ //4-D List. Packing 2 or more 3-D Matrices.
[
[
[1,2,3],
[4,5,6],
],
[
[7,8,9],
[10,11,12],
],
],
[
[
[13,14,15],
[16,17,18],
],
[
[19,20,21],
[22,23,24],
]
]
]
Append / Concatenate
///////Append List//////////
a = [1,2]
a = a + [4,5]
print(a) #[1,2,3,4]
///////Function///////////
a.append(6)
List Comprehension
-
Create new lists where each element is the result of some operation in just 1 line or few lines, rather than writing whole function. Examples
A list comprehension consists of:
- Brackets containing an expression
- followed by a for clause, then zero or more for or if clauses.
- The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.
1. List of Squares
sq = []
for i in range(5):
sq.append(i**2) #** is power operator
print(sq) #0 1 4 9 16
sq1 = [i**2 for i in range(5)] #0 1 4 9 16 #List Comprehension
2. Combining elements of 2 lists if they are not equal
com = []
for x in [1,2,3]:
for y in [3,1,4]:
if x != y:
com.append((x, y))
print([(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]) #List Comprehension
#[(1, 3), (1, 4), (2, 3), (2, 1), (2, 4), (3, 1), (3, 4)]
3. List of double value
l = [-4, -2, 0, 2, 4]
print([i*2 for i in l]) #[-8.-4.0,4,8]
print([abs(i) for i in l]) #Applying function to all values
4. Create List containing all combinations of Cubiod sides not equal to n
Question: Given 3 sides of cubiod(x,y,z) and n. Print all combinations where (x+y+z != n)
Input: x=1,y=1,z=1,n=2
Output:[[0, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0], [1, 1, 1]]
if __name__ == '__main__':
x = int(input()) #length
y = int(input()) #breadth
z = int(input()) #height
n = int(input())
print (
[
[a,b,c] #Expression
for a in range(x+1) #for clause
for b in range(y+1)
for c in range(z+1)
if a + b + c != n #if clause
]
)
Nesting of list
-
Creating a List contaning other lists
a = [1,2,3]
x = ['a','b','c']
y = [a,x]
print(y) #[[1, 2, 3], ['a', 'b', 'c']]
print(y[0]) #[1, 2, 3]
print(y[0][1]) #[2]
Insert in list
if __name__ == '__main__':
l = [] //declare list
index = 1
element = 2
l.insert(index, element)
Sort
///////// sort //////////
# Example list of lists
list_of_lists = [[3, 1, 2], [7, 4, 5], [6, 9, 8]]
# Sort the list of lists based on the first element of each sublist (ascending order)
list_of_lists.sort() # [[3, 1, 2], [6, 9, 8], [7, 4, 5]]
////////// custom sort ///////////////
# Example list of lists
list_of_lists = [[3, 1, 2], [7, 4, 5], [6, 9, 8]]
# Define a custom comparator function to sort based on the second element of each sublist (ascending order)
def custom_comparator(s):
return s[1]
list_of_lists = sorted(list_of_lists, key=custom_comparator)
# [[3, 1, 2], [7, 4, 5], [6, 9, 8]] # sorted based on 2nd element
Slice
# Getting sublist from bigger list. Slice assignment is also possible.
print(a[-3:]) #[5,6,7]
print(a[:]) #[1,2,3,4,5,6,7]
a[1:3]=[10,11] #Slice Assignment
print(a) #[1, 10, 11, 4, 5, 6, 7]
a[1:3]=[2,3]
List as Stack(LIFO)
-
List can be very easily used as a stack. Implementation:
- Consider list's end as top.
- push element at top of stack:
- list.append(x) Appends element at end
- pop element: list.pop() removes element from end
stack = [1,2,3]
stack.append(4) #[1,2,3,4]
stack.append(5) #[1,2,3,4,5]
print(stack.pop()) #5 //Remaning List[1,2,3,4]
print(stack.pop()) #4 //Remaning List[1,2,3]
List as Queue(FIFO)
-
Lists are not efficient as queues. Why lists not good queues?
Push=Slow: Inserts or pops from the beginning of a list is slow, because all of the other elements have to be shifted by one.
Pop=Fast: appends and pops from the end of list are fast