Head and Tail Function in Python

In Python, the head() function is typically used to retrieve the first n items from a list or a DataFrame.

head() function for lists

In Python, you can use the head() function with lists to retrieve the first n items in the list. The head() function is not a built-in function in Python, but you can easily create your own function to achieve the same functionality. Here's an example:

This function takes a list lst and an integer n as input and returns the first n items in the list. Here's an example usage:

head() function for DataFrames

In Python, you can also use the head() function with pandas DataFrames to retrieve the first n rows of a DataFrame. The head() function is a built-in function in the pandas library, so you don't need to create your own function. Here's an example:

Output:

name  age
0     Alice   25
1       Bob   30
2  Charlie   35

In this example, we created a DataFrame with two columns (name and age) and five rows. After that, we used the head() function to retrieve the first three rows of the DataFrame.

In Python, the tail function is used to extract a specified number of items from the end of a sequence, such as a list or a string. The resulting sequence contains only the items at the end of the original sequence, up to the specified number of items.

Here is an example of using the tail function on a list in Python:

Output:

[3, 4, 5]

In this example, the tail function takes two arguments: seq and n. seq is the sequence (in this case, a list) from which we want to extract the items at the end, and n is the number of items we want to extract.

After that, the function uses Python's slicing syntax to extract the last n items of the sequence. The -n: notation means "take the last n items", so seq[-n:] returns a new list containing only the last three items of my_list.

Note that the original list my_list is not modified by the tail function. Instead, the function returns a new list that contains only the specified number of items from the end of the original list.

This tail function can be used in several ways:

For example, when we need to extract N characters from the end of a string, which can be implemented as follows:

Output:

world!

In this example, tail(my_string, 6) returns the last six characters of the string, which are "world!".

Example 2: Extracting the Last N Lines from a File

Suppose you have a text file and want to extract the last n lines from it. You can use the tail function along with Python's built-in open function to read the file and extract the last n lines:

Output:

['Line 6\n', 'Line 7\n', 'Line 8\n', 'Line 9\n', 'Line 10\n']

In this example, tail(file_path, 5) reads the file at the specified path and returns the last five lines of the file.

Example 3: Using Tail with Other Functions

The tail function can be combined with other Python functions to perform more complex operations. For example, suppose you have a list of numbers and you want to calculate the moving average of the last n numbers. You can use the tail function along with Python's built-in sum function to calculate the sum of the last n numbers:

Output:

8.0

In this example, moving_average(my_list, 3) uses the tail function to extract the last three numbers from the list, then calculates their average using the sum function.






Latest Courses