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

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Predict Census Income using Deep Learning Models
In this project, we are going to work on Deep Learning using H2O to predict Census income.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Sequence Classification with LSTM RNN in Python with Keras
In this project, we are going to work on Sequence to Sequence Prediction using IMDB Movie Review Dataset​ using Keras in Python.

Resume parsing with Machine learning - NLP with Python OCR and Spacy
In this machine learning resume parser example we use the popular Spacy NLP python library for OCR and text classification.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

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.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

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.

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.