Lists in Python

Search for a command to run...

No comments yet. Be the first to comment.
YAML Ain't a Markup Language. This is the full form of YAML. Why YAML ? YAML is a serialization language. Means that it is used to transfer the data from one end to other like from the backend to the frontend and it is also used to store data in k...

Creating Modules As our program grows we should split our code across multiple files. We refer to each file as a module. A module should contain related data, functions, and objects. def calc_tax(): pass def calc_shipping(): pass Importi...

Step 1: Install it sudo npm i -g npkill Or you can use it without installing with npx npx npkill Step 2: Go the directory where you want to scan for node_modules folder npkill # if you have installed it npx npkill # if you have not install...

Classes All the functions in a class should have at least one parameter and by default, we call it self. self is a reference to the current object # CREATEING CLASS class Point: def draw(self): print("draw") # CREATING OBJECT FROM THE...

This is not like pop notifications. This is more like a customized alert notification. Click here to visit the official GitHub page of this project. How to use react-toastify Step 1: Install it npm i react-toastify Step 2: Import it in app.js ...

# ============================
# Lits
# in lists every element can be of different type
letters = ["a", "b", "c"]
matrix = [[1,2], [3,4]]
# repeating items in a list
print ([0] * 10)
# this will print [0,0,0,0,0,0,0,0,0,0]
# concating lists
zeros = [0] * 5
combined = zeros + letters
# the lists function
numbers = list(range(20)) # as the range function gives an itterable
# this will create a list from 0 to 19
char = list("harshit saini")
[0] # access 0th element
[-1] # access 1st element from the last
[0:3] # from elements from 0 to 2
[:3] # from 0 to 2
[0:] # from 0 to end
[:] # from 0 to end
[::2] # from 0 to end and every 2nd element only
[::-1] # get all items in reverse order
like object destructuring in js
numbers = [1,2,3]
first, second, third = numbers
# the number of variables on the left
# should be equal to numbers of elements in the list
first, *others = numbers
# we get the first and every thing
# else is packed inside others list
# this is called packing
first, *other, last = numbers
# this is how we can take the first and the last element
l = list("harshit-saini")
for ch in l:
print(ch)
# this way the ch will have only elements in the list
# and it will print only the element
l = list("harshit-saini")
for ch in enumerate(l):
print(ch)
print ("index is" , ch[0])
print ("element is ", ch[1])
# this way ch will be a tuple which contains the
# index and the element itself
# adding and removing items
g = [1,2,3,4,5]
g.append(10) # adding to the last
g.insert(0, 10) # inserting 10 at index 0
g.pop() # remove the last element
g.pop(0) # remove element fron index 0
g.remove(1) # remove 1 from the list (first occurance)
# to remove all occurance we have to loop over the list
del g[0:3] # remove a range of items from 0 to 2
del g[0] # delete the item at index 0
index = g.index(1) # get the index of 1
# note : in python if the element is not in the list and we try to print the index, we get error
# so fist we should check if the element is there and then we should find the index
if 1 in g:
print (g.index(1))
fre = g.count(12)
# this will count how many times 12 occurred in the list
numbers.sort() # this sorts in ass order
numbers.sort(reverse="True") # dess order
we can also use the built in function sorted( ). This function takes iterables as arguments. This returns a new sorted list.
new_sorted_list= sorted (numbers_list, reverse="True"
Sometimes we have to create our own sorting function to sort list like these
items = [
("Product", 10),
("Product", 9),
("Product", 12),
]
items.sort() # this will do nothing
# we have to make a function
def sort_item(item):
return item[1]
# now give this function to the sort function
items.sort(key=sort_item) # we do not have to call this function
Using Lamda Function
syntax is => lamda parameters:expression
items.sort( key=lamda item:item[1] )
# we do not need the return keyword