Loop two iterators with the zip function - Deepstash

Loop two iterators with the zip function

I love the zip function and it has saved me countless (nested) loops. I use it mostly for iterating over two data types at the same time, where I need the indexes to be equal.

You can do this with any data type or generator. For instance, you could create dictionaries without looping over the separate lists, as such:

Later on, we’ll introduce the *args operator, which in combination with the zip function is very powerful!

STASHED IN:

1

STASHED IN:

0 Comments

MORE IDEAS FROM Seven Tips To Clean Code With Python

Dictionaries are great data-types for storing values with an attribute field known as the key , in so-called key-value pairs . When extracting key-value pairs from dictionaries, avoid running into KeyError exceptions with the .get method instead of the more traditional [key] method. The .get method provides a default value if the key is not present.

In line 6-10 our program stops running because of the KeyError exception, but in line 11-14 our program continues, using the default 'undefined' string that is set in line 12 as the second argument of the .get method.

When you have the possibility of assigning a default value for accessing key-value pairs in a dictionary, always try to use the .get method to avoid your program from stopping prematurely, especially when in production.

STASHED IN:

1

Unpacking variables are probably most used for functions that return multiple variables, such as in the example below.

But it is also useful for data types that contain multiple items. The only important notion here is that, if not otherwise defined, variable unpacking results in tuples .

The _ operator is an unnamed variable , essentially a variable that you’re not interested in and won’t be doing anything with, for instance in the following case:

STASHED IN:

1

the * prefix operator was added to the multiple assignment syntax, allowing us to unpack the remaining items in an iterable.

The ** operator does something similar, but with keyword arguments. The ** operator allows us to take a dictionary of key-value pairs and unpack it into keyword arguments in a function call.

STASHED IN:

1

Making your Python code as re-usable as possible should be one of your main concerns. But what if you’re working on a Unix platform and your colleague is working on Windows?

The path delimiter on Windows is \ , but on my Linux or Mac system, it is / . Avoid dealing with these nuances by using the built-in os library:

STASHED IN:

1

List, tuple, and dictionary comprehensions are ways to code more efficiently: do the same in fewer lines of code.

Both lines 2-4 and lines 13-15 are compressed in single-line expressions in line 6 and line 17 . This saves up unnecessary loops and creates a cleaner codebase.

STASHED IN:

1

Hallelujah! That is what I thought when I learned about the Python 3.6+ update that includes a new way of formatting strings: the Python formatted string literal. String formatting in Python has come a long way.

F-strings consider everything within { curly brackets } as an expression, and with these expressions, we can do simple arithmetic but also functions and method calls!

1

STASHED IN:

2

Deepstash helps you become inspired, wiser and productive, through bite-sized ideas from the best articles, books and videos out there.

GET THE APP:

RELATED IDEA

Power of python recursion

Recursive function for calculating n! implemented in Python:

Behind the scenes, each recursive call adds a stack frame (containing its execution context) to the call stack until we reach the base case. Then, the stack begins to unwind as each call returns its results:

A demonstration should make things clearer. Let’s calculate 1 + 2 + 3 ⋅⋅⋅⋅ + 10 using recursion. The state that we have to maintain is (current number we are adding, accumulated sum till now) .

2

STASHED IN:

5

  • Math enables you to select the right machine learning algorithm. It gives insight into how the model works, including selecting the right model parameter and validation strategies.
  • Maths helps with creating the right confidence interval and uncertainty measurements with the model.
  • Maths is needed to understand aspects such as metrics, training time, model complexity, number of parameters, and number of features.
  • By knowing the machine learning model's math, you could develop a customised model.

9

STASHED IN:

74

Pandas is a Python language package, which is used for data processing. This is a very common basic programming library when we use Python language for machine learning programming. This article is an introductory tutorial to it. Pandas provide fast, flexible and expressive data structures with the goal of making the work of “relational” or “marking” data simple and intuitive. It is intended to be a high-level building block for actual data analysis in Python.

Pandas is suitable for many different types of data, including:

  • Table data with heterogeneous columns, such as SQL tables or Excel data.
  • Ordered and unordered (not necessarily fixed frequency) time series data.
  • Any matrix data with row and column labels (even type or different types)
  • Any other form of observation/statistical data set.

The Pandas Index object contains metadata describing the axis. When creating a Series or DataFrame, the array or sequence of tags is converted to Index. You can get the Index object of the DataFrame column and row in the following way:

3

STASHED IN:

21