How to append output of a for loop in a python list?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to append output of a for loop in a python list?

How to append output of a for loop in a python list?

This recipe helps you append output of a for loop in a python list

0

Recipe Objective

In python, while operating on list, we might need to store each loop output with each iteration.

So this recipe is a short example on how to append output of for loop in a python list. Let's get started.

Step 1 - Setup the Data

numbers=[10,20,30,40]

Let us create a list containing some numbers.

Step 2 - Appending list in a for loop

for i in range(5,11): numbers.append(10*i)

We use append function to append a list. Here, with each loop running, the list gets appended.

Step 3 - Printing results

print(numbers)

Simply use print function to print new appended list.

Step 4 - Let's look at our dataset now

Once we run the above code snippet, we will see:

[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]

Relevant Projects

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Build an Image Classifier for Plant Species Identification
In this machine learning project, we will use binary leaf images and extracted features, including shape, margin, and texture to accurately identify plant species using different benchmark classification techniques.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.