Indexing of Python List

12 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.22
List Price:  €93.18
You save:  €27.95
£56.04
List Price:  £80.07
You save:  £24.02
CA$96.09
List Price:  CA$137.28
You save:  CA$41.18
A$106.44
List Price:  A$152.07
You save:  A$45.62
S$94.93
List Price:  S$135.63
You save:  S$40.69
HK$547
List Price:  HK$781.46
You save:  HK$234.46
CHF 63.65
List Price:  CHF 90.94
You save:  CHF 27.28
NOK kr765.05
List Price:  NOK kr1,092.98
You save:  NOK kr327.92
DKK kr486.48
List Price:  DKK kr695
You save:  DKK kr208.52
NZ$116.68
List Price:  NZ$166.69
You save:  NZ$50.01
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,685.71
List Price:  ৳10,980.05
You save:  ৳3,294.34
₹5,844.71
List Price:  ₹8,349.95
You save:  ₹2,505.23
RM331.80
List Price:  RM474.03
You save:  RM142.22
₦98,423.43
List Price:  ₦140,610.93
You save:  ₦42,187.50
₨19,474.95
List Price:  ₨27,822.55
You save:  ₨8,347.60
฿2,584.73
List Price:  ฿3,692.63
You save:  ฿1,107.90
₺2,255.06
List Price:  ₺3,221.65
You save:  ₺966.59
B$356.31
List Price:  B$509.04
You save:  B$152.73
R1,298.99
List Price:  R1,855.78
You save:  R556.79
Лв127.57
List Price:  Лв182.26
You save:  Лв54.68
₩95,950.21
List Price:  ₩137,077.60
You save:  ₩41,127.39
₪261.40
List Price:  ₪373.45
You save:  ₪112.04
₱4,015.50
List Price:  ₱5,736.67
You save:  ₱1,721.17
¥10,912.42
List Price:  ¥15,589.84
You save:  ¥4,677.42
MX$1,187.01
List Price:  MX$1,695.81
You save:  MX$508.79
QR254.79
List Price:  QR364.01
You save:  QR109.21
P956.45
List Price:  P1,366.42
You save:  P409.96
KSh9,168.69
List Price:  KSh13,098.69
You save:  KSh3,930
E£3,313.80
List Price:  E£4,734.21
You save:  E£1,420.40
ብር4,020.93
List Price:  ብር5,744.43
You save:  ብር1,723.50
Kz58,536.36
List Price:  Kz83,626.96
You save:  Kz25,090.59
CLP$65,519.73
List Price:  CLP$93,603.63
You save:  CLP$28,083.90
CN¥505.79
List Price:  CN¥722.59
You save:  CN¥216.80
RD$4,064.05
List Price:  RD$5,806.04
You save:  RD$1,741.98
DA9,434.16
List Price:  DA13,477.95
You save:  DA4,043.79
FJ$159.43
List Price:  FJ$227.77
You save:  FJ$68.34
Q544.12
List Price:  Q777.35
You save:  Q233.22
GY$14,659.33
List Price:  GY$20,942.80
You save:  GY$6,283.47
ISK kr9,804.19
List Price:  ISK kr14,006.59
You save:  ISK kr4,202.40
DH702
List Price:  DH1,002.91
You save:  DH300.90
L1,236.68
List Price:  L1,766.77
You save:  L530.08
ден4,019.08
List Price:  ден5,741.79
You save:  ден1,722.70
MOP$563.89
List Price:  MOP$805.60
You save:  MOP$241.70
N$1,302.92
List Price:  N$1,861.40
You save:  N$558.47
C$2,577.98
List Price:  C$3,682.99
You save:  C$1,105
रु9,357.62
List Price:  रु13,368.60
You save:  रु4,010.98
S/260.83
List Price:  S/372.63
You save:  S/111.80
K271.34
List Price:  K387.65
You save:  K116.30
SAR262.49
List Price:  SAR375
You save:  SAR112.51
ZK1,913.50
List Price:  ZK2,733.69
You save:  ZK820.18
L324.50
List Price:  L463.60
You save:  L139.09
Kč1,630.34
List Price:  Kč2,329.15
You save:  Kč698.81
Ft25,331.89
List Price:  Ft36,189.97
You save:  Ft10,858.07
SEK kr764.93
List Price:  SEK kr1,092.80
You save:  SEK kr327.87
ARS$61,714.55
List Price:  ARS$88,167.42
You save:  ARS$26,452.87
Bs483.86
List Price:  Bs691.26
You save:  Bs207.40
COP$272,553.83
List Price:  COP$389,379.31
You save:  COP$116,825.47
₡35,845.57
List Price:  ₡51,210.15
You save:  ₡15,364.58
L1,730.57
List Price:  L2,472.36
You save:  L741.78
₲523,213.21
List Price:  ₲747,479.48
You save:  ₲224,266.27
$U2,704.33
List Price:  $U3,863.50
You save:  $U1,159.16
zł280.23
List Price:  zł400.34
You save:  zł120.11
Already have an account? Log In

Transcript

Welcome, everybody. So in this tutorial, we'll go through some of the ways that we can work with least in Python. First we'll do indexing of a list. So each item in a list corresponds to an index number, which is an integer value, starting with the index number zero. So can you recall what we have done in our previous video we have created a list called creator. Which content item suppose well.

Octopus sweet, sharp again Shem So, here's a list that I've created and printed we all know this. See it gives me error quite give me error and tell me name creature is not defined. Because here I have written something wrong. See here I've written missed out over here. So basically just typing error. So now the list is created.

Now. For the list creator, the index number if we break down it looked like this Well, is of index number zero. Octopus of index number one, squid of index number two, shot with index number three, and shrimp with index number four to the first item. That is the string We'll start at index zero, and then least aim at index four with the item. Shin. So I have all already told you that in machine learning or in machine, the counting always start from zero, so zero, then 1234.

So if you want to call the first element of the list, you need to give the index number zero. If you want to call the second number of the second item of the list, you need to provide the index number one, so in this way, it will go on because each item in a Python list has a corresponding index number we are able to access and manipulate at least in the same way we can with other sequential database. Now, we can call a discrete item of the list by referring to our index number. So Here I'll write, print creature and I'll give a pair of square brackets and within this pair of square brackets I'll provide the index number. So, suppose I want the second element that is I want octopus. So, what index number should I provide here?

I have to provide one so just run it what it returned in return October's now if I write print creator, zero, then what we will print it will print well. So when has an index number of zero octopus era index number one, no print creature supports four then what will happen? It will return the last element that is shaped Now suppose if we call the list creature with an index number of any that is written them for what it will return. Let's just do it and see print creature and within this I will write anything greater than four. Suppose I'll write eight, then what it will return see it return error and what this error says list index out of range, because there are only this fine element starting from zero to four I am providing an index number which is out of range of this list.

So, it will give you an error and it will print that list index out of range. So, in addition to positive index number we can also access items from the list with a negative index number by counting backwards from the end of the list starting at minus one. So, this is a list is a list that we have created and if we start from backward how we will come to It will count it as minus one minus two minus three minus four and minus five. Now, it will not start from zero from backward it will not start from zero from backward it will start from minus one. So, suppose if I write here print and within this right creature within the square bracket I write minus three then what it will return it will return the third value third item from backward So, this run it what it returns quit now check this list.

This first second is third the minus one minus two and minus three. So, minus three squid I got squid as my output. Now we can concatenate string item in the list with other string using the plus operator. So, here I'll write print. Suppose I'm right here within quotes Sammy Isa, then I'll write a plus sign and then I'll write the least name along with the index number. So what index number I provide here?

Suppose I'll provide minus two. So what it will print, see what it print it prints Sammy is a shark because minus two indicates shark. Starting from Blackboard. This is minus one, this is minus two. So you have concatenate a string with a list item. So here we are able to concatenate the string at index number minus two.

With a string Sammy is a we can also use the plus operator to concatenate or two or more lists together. Suppose here I'll print print. I love this class. Men all right creature give the index number zero, then again give the constitution sign within this right and he can give a concatenation sign. And again here, right creator then right minus two, what it will print, see what print I love well and sharp. It is showing in this way because I haven't provided any space so if I provide a space within this now it will print perfectly.

I love well and shocked. so here we can concatenate two or more string with two or more list items. So with index number that corresponds to item within a list were able to access each item of the list. quickly and work with those items. So by this few functions, you are now a custom that understand that a list is very flexible. So we can use each element of the list, I will now show you how to create a nested list.

So what is nested list? nested list is a list within a list. So I'll give a name mind list and here with a pair of square brackets and right mouse is an element of a list name and create a list within the list. So here again, I'll provide a pair of square brackets and within this, I'll give you numbers 123. Suppose then give a comma and then again, I'll provide a pair of square bracket and within this I'll write again a string value. So here what we have created, so this is a list starting from here, and within this list, I'm creating two more list.

So this is called New nested lists. So just let me write it over here nested list now just run it run and print it. See how it print it for it will create or print in the same manner that you have created the list nested list so it first create mount then within this list, this is another list and within this whole list, this is one more string list. So today I'll end my video over here in the next video I'll show you how you can modify your list well then keep practicing Thank you see in the next video. So one more thing I'm showing you today here is how to use this index function for nested Li list. So in this nested list, this is the first element is the second element in this third element.

So this will be index zero, this will be one and this will be index two. Now if I write it here, print Mind list and within this I write zero then it will just print mouse. Similarly, if I print mindless and here if I write one it will print if I made a typing error, this will be my list. Now see it printed the second item that is the polling list 123. Now, suppose if I want this from the second list, if a second element I want only the this element to then what to do I have to write print my list here, here I have to mention from which element So, here I mentioned one and again hear from one which element again right one Because this is index one and within this index one, this is again index one. So I written one one.

So run it see distracted only two, that is first the first number is indicating which list then the second number is indicating in that list which element you want to extract. Now, suppose I want to print my list and here I am writing in one and within this one, I wonder this three I want to extract three So, I have to write 0122 Now see, so extracted the element three from here So, in this video we have learned how to use this index function, or indexing in a list how to extract different elements in a list and how to use each element in a list. How to concatenate a string with a with a particular icon. Have a list how to concatenate two or more string with two or more items of a list and how to create a nested list. And also you have learned how to use indexing in nested list.

So keep practicing. See you in the next video. Thank you

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.