Python: Create list and Usage for Beginners

When writing Artificial Intelligence softwares and systems, you often need to use Python to generate and compare multiple data sets. So instead of having to reference to one variable at a time, you can put these variables into a list.

Python Create List

Creating a list is similar to creating a variable and can be easily written with the equal sign.

For example, if you work on a commission basis and receive varying income per month, you can create a list that reference to your annual pay without having to calculate your annual pay individually.

#first add your individual datas into a variable

Jan=1000
Feb=2000

Mar=1500
Apr=2000
May=1000
Jun=1500
Jul=2000
Aug=2500
Sept=3000
Oct=1000
Nov=2000
Dec=1000

Next, create a list based on your stated variables by inserting the variables within a bracket as such:

Annual_Pay=[Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Nov, Dec]

and to show the respective amount

print(annual_pay)

which would return

[1000, 2000, 1500, 2000, 1000, 1500, 2000, 2500, 3000, 1000, 2000, 1000]

However this might be confusing and hard for you to know how much you are exactly earning for each month. So the good news is that if you wish to, you can also different Python types within your list such as strings (text). To differentiate your text from your variable amount, use the quotes to wrap around your text as such:

Annual_Pay=[“January“, Jan, “February,” Feb, “March”, Mar, “April”, Apr, “May”, May, “June”, jun, “July”, Jul, “August”, Aug, “September Sept, “October”, Oct, “November”, Nov, “December”, Dec]

Which would then return

[January, 1000, February, 2000, March, 1500, April, 2000, May, 1000, June, 1500, July, 2000, August, 2500, September, 3000, October, 1000, November, 2000, December, 1000]

Also, do note that a list itself can also contain formula or another list. For example, if you are commission based and you get 500 per sale. You just have to jot down how many sales you got for that month and multiple it by 500. For example

Annual_Pay=[“January“, 500*2, “February,” 500*4, “March”, 500*3]

Which will still give you the same result for Jan to March.

[January, 1000, February, 2000, March, 1500]

Or if you are working on you pay over a few years, you can generate each list per year with your respective pay as per above example in such a format: annual_pay_2016, annual_pay_2017, annual_pay_2018, annual_pay_2019, annual_pay_2020

and then create a combined list such as:

past_5_years_annual_pay= [annual_pay_2016, annual_pay_2017, annual_pay_2018, annual_pay_2019, annual_pay_2020]

Indexing in Python List

ok so back to our previous example of the list Annual_Pay. Say what if you don’t want to list down your whole year’s pay and only wish to show the salary of just one month for example. How? Index!

So in Python, the index in the list uses a zero indexing concept where the first element in the list has the index 0, the second element has the index 1 and so on.

python-create-list-index

So if the list for

Annual_Pay=[“January“, Jan, “February,” Feb, “March”, Mar, “April”, Apr, “May”, May, “June”, jun, “July”, Jul, “August”, Aug, “September Sept, “October”, Oct, “November”, Nov, “December”, Dec]

and you only want to show your pay for march. The variable Mar is the 5th element, so it has the index 4. and to print that use:

print(Annual_pay[4])

you can also print indexes from the back using negative numbers. For example based on the above example, if you want to show you pay in dec, instead of counting it all the way till the end, you can use a negative index -1

print(Annual_pay[-1])

which is also the same result as

print(Annual_pay[23])

Do note that for forward counts, the first number is index zero while if you count from the back, the first number is negative one.

Slicing Python List

Next, say you want to slice up your list and break them down, say you want to just list your salary from March to June. You can do so by using a colon and name the range through the indexes that you wish to list.

python-create-list-slicing

The rule is this the first number that you list before the colon is included but the number you list after the colon is not included.

so again, if you wish to list from March to June, you should print

print(Annual_pay[4:12])

You can opt to leave out either the before or after index like such:

print(Annual_pay[4:])

or

print(Annual_pay[:12])

for the first example, print(Annual_pay[4:]), Python will return all elements from index 4 all the way to the last element.

for the second example print(Annual_pay[:12]) , you are asking python to start from index zero and it will return index 0 all to way to index 11.

Now that you have learn how to call the respective indexes, you can also perform calculations based on the selected index.

For example:

Jan_Feb_Pay= Annual_pay[1]+Annual_pay[3]

Changing Python List

Changing elements within a list is the same as assigning a variable. You simply find the index of the element you wish to change and give it a new element. For example, you realise that you January pay is wrong and instead of 1000, its supposed to be 1500. So simply find the index of your jan pay, which is index 1, and write this:

Annual_pay[1]=1500

When you eventually do print your list, you will realise that the Jan pay will have been updated.

To change a few elements that are back to back, you can use the same slicing method taught above

Annual_pay[0:2]= [“January2020”, 1500]

Hope this helps and do reach out if you have any further questions!

Adding elements from Python List

To add elements to a list, there are 2 ways to go about doing it. The first is through simple addition. Say you wish to add the following year’s January pay which is 2000.

Annual_pay + [“Jan2021”, 2000]

Which will add Jan2021 and 2000 to the bottom of your existing Annual_pay list.

Alternatively, you can also create a new list extension such as:

Annual_pay_and_jan= Annual_pay + [“Jan2021”, 2000]

The new list Annual_pay_and_jan will now have your annual pay plus the salary of Jan the year after.

Removing elements from Python List

To remove elements from an index we use del. Say with the new list of Annual_pay_and_jan you wish to remove the previous year’s Jan salary. You can hence use

del(Annual_pay_and_jan[0:2])

But do note that once you do so, the indexes will all now be shifted down by the number of elements that was deleted.

The new index 0 and index 1 is now Feb and Feb pay

Have fun coding and do let us know if you guys got any questions!

Leave a Comment!