Now you can send infinite arguments in function calling and you don’t need to define them in the function implementation that how many arguments function can receive.
# Function argument infinitedef sum(*n): s = 0 for x in n: s=s+x print(s) # 22 sum(1, 3, 5, 6, 7)
You no longer need to iterate the dictionary and do some lengthy coding for converting dictionary to list. The below code will do the same work but in shortcode and time.
# Dictionary to Listgames = {"Rockstar":"GTA", "UBISOFT":"AC", "BT":"DOOM"}studio = list(games.keys())title = list(games.values())print(studio) # ['Rockstar', 'UBISOFT', 'BT']print(title) # ['GTA', 'AC', 'DOOM']
This is another awesome tip code that will help you get sample data from a list with any quantity. Check out the below example code.
# Get Sampleimport random#example 1mylist = [100, 200, 300, 400, 500, 600]print(random.sample(mylist, 2)) # [100, 400]#example 2mylist = ["a", "b", "c", "d", "e", "f"]print(random.sample(mylist, 3)) # ['d', 'c', 'b']
Let suppose you had a big list of elements and your task to find the distinct elements from them. So this tip code will make your life easier.
# Find Distinct elmentsdef Distinct(mylist): seen = set() return [x for x in mylist if x not in seen and not seen.add(x)]print(Distinct([1, 2, 1, 1, 3, 5, 5, 7, 9])) # [1, 2, 3, 5, 7, 9]
This tip will save your tip by iterating a list or string to reverse them. You can simply use the step operator to reverse the string or list. Check out the below code for example.
# Reverse String and list#String reversedata1 = "Hello Python"data2 = "Learn Python" print(data1[::-1]) # nohtyP olleHprint(data2[::-1]) # nohtyP nraeL#List Reversemylist = [230, 330, 430, 530]print(mylist[::-1]) # [530, 430, 330, 230]
CSV files are the important data record format and to read a CSV file in python we mostly use the Pandas module. But this tip will help you read CSV with a built-in module. Check out the below code example.
# Read Csv Filesimport csvwith open("file.csv", "r") as file: read = csv.reader(file) for row in read: print(row)# Output:['1', 'platinum', 'Muhammed MacIntyre']['2', 'Office Refrigerators', 'Barry French']['3', 'Heavy Gauge Vinyl', 'Barry French']
This amazing tip and trick will help you to make a calculator without using the If-Else condition. I had used the built-in operator module and dictionary method.
# Smart Calculator without if elseimport operatoractions = { "+" : operator.add, "-" : operator.sub, "*" : operator.mul, "/" : operator.truediv}print(actions["+"](12, 45)) # 57print(actions["-"](12, 45)) # -33
This awesome tip will help you to deep flatten your irregular list. This tip will use when you need to flatten an irregular ray that can’t flatten with a normal flatten code.
# Deep Flatten def Deep_Flatten(str):if isinstance(str[0], list): return Deep_Flatten(str[0]) + Deep_Flatten(str[1:]) return str[:1] + Deep_Flatten(str[1:])print(flatten([1,2,[3,5,[34,56]],[23]])) # [1, 2, 3, 5, 34, 56, 23]
List comprehensions are an elegant way to build a list without having to use different for loops to append values one by one. Simplest form
[expresssion-involving-loop-variable for loop-variable in sequence]
Program
# Creating list of integers from 0 to 9
ListOfNumbers = [x for x in range(10)]
print(ListOfNumbers)
Output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Enumerate() is the python method that adds a counter to an iterable and returns it in a form of enumerate object. This enumerate object can then be used directly in for loops or be converted into a list of tuples uing list() method. Syntax
enumerate(iterable, start=0)
Parameters:
iterable: any object that supports iteration
start: the index value from which the counter
is to be started, by default it is 0
Program
football_clubs = ['Barcelona', 'Real Madrid', 'Liverpool',
'Manchester United', 'Juventus']
# Classical approach
i = 0
for club in football_clubs:
i += 1
print(i, club)
# Using enumerate()
for i, club in enumerate(football_clubs, 1):
print(i, club)
Output
1 Barcelona
2 Real Madrid
3 Liverpool
4 Manchester United
5 Juventus
'''