top of page
learn_data_science.jpg

Data Scientist Program

 

Free Online Data Science Training for Complete Beginners.
 


No prior coding knowledge required!

Python Concepts for Data Science : Creating a list in python


1. Introduction

With python we can collect and stock many variables in a list. This is a series of data enclosed in brackets, whose values are separated by commas. These values can be numbers, strings or mixed. For string values, single or double quotes must be used, for example: "fox" or 'fox'. Thus, a python list can stock a set of data of variable size and of different types, which gives them a great flexibility. For a simple case we will :

2. Simple list

# Create a list that contains food prices in dollars/kilogram

# step 1 start defining the different variables

Lime = 1.2
Orange = 2.8
Apple = 3.1
Carrot = 1.6
Pastry_flour = 1.5
Chicken_breast = 11
Ground_meat = 12.1
Chicken_leg = 5.5

# step 2 create a list variable called 'Food_lyst' which will contain all the variables listed on it

Food_lyst = [Lime, Orange, Apple, Carrot, Pastry_flour, Chicken_breast, Ground_meat, Chicken_leg]

print(Food_lyst)
[1.2  ,  2.8  , 3.1 , 1.6 , 1.5 , 11 , 12.1 , 5.5] 

3. Mixed list

Once printed, the list ''Food_lyst'' gives only values that correspond to the prices of foods products. This list can be improved by adding the names of foods products with the price that corresponds to each product. So, we will build a list so that the list contains first the name of the product as a string, then its price. In other words, add the strings "Lime", "Orange", "Apple", "Carrot", "Pastry_flour", "Chicken_breast", "Ground_meat", "Chicken_leg


Food_prices = [" Lime", Lime, "Orange", Orange, "Apple", Apple, "Carrot", Carrot, "Pastry_flour", Pastry_flour, "Chicken_breast", Chicken_breast, "Ground_meat", Ground_meat, "Chicken_leg", Chicken_leg]

print(Food_prices)

[' Lime', 1.2, 'Orange', 2.8, 'Apple', 3.1, 'Carrot', 1.6, 'Pastry_flour', 1.5, 'Chicken_breast', 11, 'Ground_meat', 12.1, 'Chicken_leg', 5.5]

4. Accessibility of the values in the list

One of the advantages of a python list is that we can call its elements by their position. That is, after creating a list of values containing "n" elements (integer, string etc.), these values are automatically associated with numbers ranging from 0 to n-1 in order of entry or from -n to -1 in the opposite direction. These numbers are called "indices" or "indexes" of the list.



Thus, we can easily call a value or values with the following formula:


print(Food_prices[4]) 
"Apple"
Print(Food_prices[-2])
"Chicken_leg"
Pint(Food_prices[0:6]
[" Lime", 1.2, "Orange", 2.8, "Apple", 3.1]

5. Put lists in a list

We can also put this list in the form of a list in list by putting square brackets between the variables. For example in our case we can put between brackets the name of each product and its price. Thus, each product with its price represents a list.


Food_prices = [[" Lime", Lime] , ["Orange", Orange] , ["Apple", Apple] , ["Carrot", Carrot] , ["Pastry_flour" , Pastry_flour] , ["Chicken_breast", Chicken_breast] , ["Ground_meat", Ground_meat] , ["Chicken_leg", Chicken_leg]]

print(Food_prices)

[[' Lime', 1.2], ['Orange', 2.8], ['Apple', 3.1], ['Carrot', 1.6], ['Pastry_flour', 1.5], ['Chicken_breast', 11], ['Ground_meat', 12.1], ['Chicken_leg', 5.5]]

2件のコメント


Data Insight
Data Insight
2021年10月21日

You should embed code to your post using any of the methods described here linktrest.io/EmbedCode

いいね!

Data Insight
Data Insight
2021年10月21日

Please delete every screen shot of code in your post. Otherwise your whole article will be deleted.

いいね!

COURSES, PROGRAMS & CERTIFICATIONS

 

Advanced Business Analytics Specialization

Applied Data Science with Python (University of Michigan)

Data Analyst Professional Certificate (IBM)

Data Science Professional Certificate (IBM)

Data Science Specialization (John Hopkins University)

Data Science with Python Certification Training 

Data Scientist Career Path

Data Scientist Nano Degree Program

Data Scientist Program

Deep Learning Specialization

Machine Learning Course (Andrew Ng @ Stanford)

Machine Learning, Data Science and Deep Learning

Machine Learning Specialization (University of Washington)

Master Python for Data Science

Mathematics for Machine Learning (Imperial College London)

Programming with Python

Python for Everybody Specialization (University of Michigan)

Python Machine Learning Certification Training

Reinforcement Learning Specialization (University of Alberta)

Join our mailing list

Data Insight participates in affiliate programs and may sometimes get a commission through purchases made through our links without any additional cost to our visitors.

bottom of page