Featured Post

Mastering flat_map in Python with List Comprehension

Image
Introduction In Python, when working with nested lists or iterables, one common challenge is flattening them into a single list while applying transformations. Many programming languages provide a built-in flatMap function, but Python does not have an explicit flat_map method. However, Python’s powerful list comprehensions offer an elegant way to achieve the same functionality. This article examines implementation behavior using Python’s list comprehensions and other methods. What is flat_map ? Functional programming  flatMap is a combination of map and flatten . It transforms the collection's element and flattens the resulting nested structure into a single sequence. For example, given a list of lists, flat_map applies a function to each sublist and returns a single flattened list. Example in a Functional Programming Language: List(List(1, 2), List(3, 4)).flatMap(x => x.map(_ * 2)) // Output: List(2, 4, 6, 8) Implementing flat_map in Python Using List Comprehension Python’...

How to Find Max and Min Quickly in Python List

Here's logic to find Max and Min values in a List. Without using built-in Max() and Min () functions you can find Max and Min values in a List. For that matter, you need to write a user-defined function. This post is all about how to write it and run.

Finding Max and Min in List


You can achieve this by writing a user-defined function. Here's useful logic and steps to write it precisely.


IN THIS PAGE

  • Write a Function
  • Execute Function
  • Get the result
  • Write Function

You can write an user-defined function to get MAX and MIN values. In this function, I am using the max(), min() built-in functions. The other variables you can use as you wish. Below is the my actual logic.


Python Function to find Maximum and Minimum Easily


Sample Code

def findmaxmin(data): 
ax = max(data) 
by = min(data) 
return(x,y)
data = (109, 98, 88, 7) 
(maximum, minimum) = findmaxmin (data) 
print("Maximum Marks = ", maximum) 
print("Minimum Marks = ", minimum)


Created a Script Using vim editor

Here as the first step, I have written the maxmin.py file in Ubuntu. The function name is  "findmaxmin ".

define function using input arguments

Result


Below command to enter Python. This command, you can issue in Ubuntu where python3 is installed

$ python3

You have now entered Python3.

>>> import maxmin

You will prompt a message that imported successfully without any syntax errors.

Code in the Python Script


How to import python script and run


Below commands, I have issued.

>>> data = (109, 98, 88, 7)
>>> maxmin.findmaxmin(data)


The result is (109, 7)


References

Comments

Popular posts from this blog

SQL Query: 3 Methods for Calculating Cumulative SUM

Big Data: Top Cloud Computing Interview Questions (1 of 4)

5 SQL Queries That Popularly Used in Data Analysis