Sorts one list based on another list containing the desired indexes.
Use zip() and sorted() to combine and sort the two lists, based on the values of indexes.
Use a list comprehension to get the first element of each pair from the result.
Use the reverse parameter in sorted() to sort the dictionary in reverse order, based on the third argument.
CODE:
a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk']
b = [3, 2, 6, 4, 1, 5]
sort_by_indexes(a, b)
Output: ['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges']
sort_by_indexes(a, b, True)
Output: ['oranges', 'milk', 'jam', 'eggs', 'bread', 'apples']
Share and Support
@Python_Codes
Use zip() and sorted() to combine and sort the two lists, based on the values of indexes.
Use a list comprehension to get the first element of each pair from the result.
Use the reverse parameter in sorted() to sort the dictionary in reverse order, based on the third argument.
CODE:
def sort_by_indexes(lst, indexes, reverse=False):
return [val for (_, val) in sorted(zip(indexes, lst), key=lambda x: \
x[0], reverse=reverse)]
Example:a = ['eggs', 'bread', 'oranges', 'jam', 'apples', 'milk']
b = [3, 2, 6, 4, 1, 5]
sort_by_indexes(a, b)
Output: ['apples', 'bread', 'eggs', 'jam', 'milk', 'oranges']
sort_by_indexes(a, b, True)
Output: ['oranges', 'milk', 'jam', 'eggs', 'bread', 'apples']
Share and Support
@Python_Codes
unfold
Builds a list, using an iterator function and an initial seed value.
The iterator function accepts one argument (seed) and must always return a list with two elements ([value, nextSeed]) or False to terminate.
Use a generator function, fn_generator, that uses a while loop to call the iterator function and yield the value until it returns False.
Use a list comprehension to return the list that is produced by the generator, using the iterator function.
CODE:
INPUT:
unfold(f, 10)
OUTPUT:
[-10, -20, -30, -40, -50]
Share and Support
@Python_Codes
Builds a list, using an iterator function and an initial seed value.
Use a generator function, fn_generator, that uses a while loop to call the iterator function and yield the value until it returns False.
Use a list comprehension to return the list that is produced by the generator, using the iterator function.
def unfold(fn, seed):f = lambda n: False if n > 50 else [-n, n + 10]
def fn_generator(val):
while True:
val = fn(val[1])
if val == False: break
yield val[0]
return [i for i in fn_generator([None, seed])]
INPUT:
unfold(f, 10)
OUTPUT:
[-10, -20, -30, -40, -50]
Share and Support
@Python_Codes
curry
Curries a function.
Use functools.partial() to return a new partial object which behaves like fn with the given arguments, args, partially applied.
CODE:
@Python_Codes
Curries a function.
from functools import partial
def curry(fn, *args):
return partial(fn, *args)
Examples:add = lambda x, y: x + y
add10 = curry(add, 10)
add10(20) # 30
Share and Support@Python_Codes
From Today we start from basic useful Python codes which are useful to everyone while coding in python
#Basics
Return Multiple Values From Functions.
Code:
print(a, b, c, d)
Output:
1 2 3 4
Share and Support
@Python_Codes
Return Multiple Values From Functions.
Code:
def x():Input:
return 1, 2, 3, 4
a, b, c, d = x()
print(a, b, c, d)
Output:
1 2 3 4
Share and Support
@Python_Codes
#Basics
Find The Most Frequent Value In A List
Code:
4
Share and Support
@Python_Codes
Find The Most Frequent Value In A List
Code:
test = [1, 2, 3, 4, 2, 2, 3, 1, 4, 4, 4]Output:
print(max(set(test), key = test.count))
4
Share and Support
@Python_Codes
#Basics
Check The Memory Usage Of An Object.
Code:
28
Share and Support
@Python_Codes
Check The Memory Usage Of An Object.
Code:
import sysOutput:
x = 1
print(sys.getsizeof(x))
28
Share and Support
@Python_Codes
#Basics
Checking if two words are anagrams
Code:
print(is_anagram('code', 'doce'))
print(is_anagram('python', 'yton'))
Output:
True
False
Share and Support
@Python_Codes
Checking if two words are anagrams
Code:
from collections import Counter
def is_anagram(str1, str2):
return Counter(str1) == Counter(str2)
# or without having to import anything
def is_anagram(str1, str2):
return sorted(str1) == sorted(str2)
print(is_anagram('code', 'doce'))
print(is_anagram('python', 'yton'))
Output:
True
False
Share and Support
@Python_Codes
#Basics
zip() function
When we need to join many iterator objects like lists to get a single list we can use the zip function. The result shows each item to be grouped with their respective items from the other lists.
Example:
[(1999, 'Mar', 11), (2003, 'Jun', 21), (2011, 'Jan', 13), (2017, 'Dec', 5)]
Share and Support
@Python_Codes
zip() function
Year = (1999, 2003, 2011, 2017)Output:
Month = ("Mar", "Jun", "Jan", "Dec")
Day = (11,21,13,5)
print zip(Year,Month,Day)
[(1999, 'Mar', 11), (2003, 'Jun', 21), (2011, 'Jan', 13), (2017, 'Dec', 5)]
Share and Support
@Python_Codes
#Basics
Transpose a Matrix
Transposing a matrix involves converting columns into rows. In python we can achieve it by designing some loop structure to iterate through the elements in the matrix and change their places or we can use the following script involving zip function in conjunction with the * operator to unzip a list which becomes a transpose of the given matrix.
Example:
[(31, 40, 13), (17, 51, 12)]
Share and Support
@Python_Codes
Transpose a Matrix
x = [[31,17],Output:
[40 ,51],
[13 ,12]]
print (zip(*x))
[(31, 40, 13), (17, 51, 12)]
Share and Support
@Python_Codes
from turtle import *
color('red', 'green')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
@python_codes
color('red', 'green')
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) < 1:
break
end_fill()
done()
@python_codes
#Basics
The _ Operator
The _ operator might be something that you might not have heard of. Here _ is the output of the last executed expression. Let’s check how it works.
Example:
5
# the _ operator, it will return the output of the last executed statement.
Share and Support
@Python_Codes
The _ Operator
>>>
2+ 35
>>>
_ >>>
5Share and Support
@Python_Codes
#Basics
Swap keys and values of a dictionary
dictionary =
Output:
@Python_Codes
Swap keys and values of a dictionary
dictionary =
{"a": 1, "b": 2, "c": 3}
reversed_dictionary = {j: i for i, j in dictionary.items()}
print(reversed)
Output:
{1: 'a', 2: 'b', 3: 'c'}
Share and Support@Python_Codes
🕹 Want to work in the Gaming industry?
We collected jobs for python 🐍 developers in the Gaming industry in India 🇮🇳
Click the link below to see them in our telegram channel.
👉 Click here: https://www.tg-me.com/+ORGwFAgg2HliZDNi
We collected jobs for python 🐍 developers in the Gaming industry in India 🇮🇳
Click the link below to see them in our telegram channel.
👉 Click here: https://www.tg-me.com/+ORGwFAgg2HliZDNi
#Basics
Condition inside the print function
is_positive(-3)
Output:
Negative
Share and Support
@Python_Codes
Condition inside the print function
def is_positive(number):
print("Positive" if number > 0 else "Negative")
is_positive(-3)
Output:
Negative
Share and Support
@Python_Codes
#Basics
Convert a value into a complex number
(10+2j)
Share and Support
@Python_Codes
Convert a value into a complex number
print(complex(10, 2))Output:
(10+2j)
Share and Support
@Python_Codes