List Comprehension Python List of Lists

[20-SEC SUMMARY] Given a list of list stored in variable lst.

To flatten a list of lists, use the list comprehension statement [x for l in lst for x in l]. To modify all elements in a list of lists (e.g., increment them by one), use a list comprehension of list comprehensions [[x+1 for x in l] for l in lst].

List comprehension is a compact way of creating lists. The simple formula is [ expression + context ].

Expression: What to do with each list element?Context: What list elements to select? It consists of an arbitrary number of for and

Continue reading the original article

Regular Expressions: Regexes in Python, Part 1

In this tutorial, you’ll explore regular expressions, also known as regexes, in Python. A regex is a special sequence of characters that defines a pattern for complex string-matching functionality.
Earlier in this series, in the tutorial Strings and Character Data in Python, you learned how to define and manipulate string objects. Since then, you’ve seen some ways to determine whether two strings match each other:

You can test whether two strings are equal using the equality (==) operator.

You can test whether one string is a substring of another with the in operator or the built-in string methods .find() and .index().

String matching like

Continue reading the original article

How to Convert a List of Lists to a CSV File in Python

Problem: How to convert a list of lists to a csv file?

Example: Given is a list of list—for example salary data of employees in a given company:

salary = [[‘Alice’, ‘Data Scientist’, 122000],
[‘Bob’, ‘Engineer’, 77000],
[‘Ann’, ‘Manager’, 119000]]

Your goal is to write the content of the list of lists into a comma-separated-values (CSV) file format. Your outfile should look like this:

# file.csv
Alice,Data Scientist,122000
Bob,Engineer,77000
Ann,Manager,119000

Solution: There are four simple ways to convert a list of lists to a CSV file in Python.

CSV: Import the csv module in Python, create a csv writer object, and write the list of lists to the

Continue reading the original article

Pandas to_csv()

You can convert a list of lists to a Pandas DataFrame that provides you with powerful capabilities such as the to_csv() method. This is the easiest method and it allows you to avoid importing yet another library (I use Pandas in many Python projects anyways).

salary = [[‘Alice’, ‘Data Scientist’, 122000],
[‘Bob’, ‘Engineer’, 77000],
[‘Ann’, ‘Manager’, 119000]]

# Method 2
import pandas as pd
df = pd.DataFrame(salary)
df.to_csv(‘file2.csv’, index=False, header=False)

Output:

# file2.csv
Alice,Data Scientist,122000
Bob,Engineer,77000
Ann,Manager,119000

You create a Pandas DataFrame—which is Python’s default representation of tabular data. Think of it as an Excel spreadsheet within your code (with rows and columns).

The DataFrame is a very powerful data

Continue reading the original article

Python 101: Conditional Statements

Developers have to make decisions all the time. How do you approach this problem? Do you use technology X or technology Y? Which programming language(s) can you use to solve this? Your code also sometimes needs to make a decision.
Here are some common things that code checks every day:

Are you authorized to do that?
Is that a valid email address
Is that value valid in that field?

These sorts of things are controlled using conditional statements. This topic is usually called control flow. In Python, you can control the flow of your program using if, elif and else statements. You can also do

Continue reading the original article