Python Basics

Python List

List in Python programming is a type of variable which holds one or more than one elements.

List Declaration

We can use any name that has no space or any special character to declare a variable of type list.

Example

fruitnames=[“Orange”,”Banana”,”Grape”,”Pine Apple”,”Jackfruit”]

roll_numbers=[101,102,103,104,105]

While declaring list, square brackets are important. The starting square bracket, and closing square bracket defines the list. All the element values are separated by comma.

Note: The last element in the list does not have comma. It is important. We use comma only to separate elements.

In above we have learned how to declare a list variable and set values.

Now, how can we print the values or elements of the list?

Answer: By using index number. In the above example, fruitnames is list variable which has five different fruit names. The first fruit name is Orange, the second fruit name is Banana, the third fruit name is Grape, the fourth fruit name is Pine Apple and fifthe fruit name is Jackfruit.

So the index number of a list variable is kind of a roll number. In school, we have roll numbers such as 1, 2, 3, 4 and so on. Similarly each element in a list has roll number and by calling that roll number we can get that respective element from the list.

So If I want to know what is the first fruit name in the fruitnames list variable then I need to call the index number of first element in the list.

So it means I need to call 1 to get the first fruit name in the fruitnames variable. But it is wrong. And the reason is, in Mathematics or in general life the number count begins from 1, 2, 3, 4 and so on. But in programming world the number count begins from zero. 0,1,2,3,4 and so on.

Therefore the first index number of any element in a list is zero.

So to print the first element of a list, I need to call zero index number.

Therefore the first fruit name in the fruitnames list variable is

print(fruitnames[0])

Output: Orange

Similarly the first number in the roll_numers list variable is

print(roll_numbers[0])

Output: 101

Similar fashion:

The second fruit name in the fruitnames variable is

print(fruitnames[1])

Output:Banana

Similar fashion

The third element in roll_numbers variable is

print(roll_numbers[2])

Output: 103

Can you guess, what will happen if I run this code print(fuitnames[5])?

Answer: It will throw an error. Because the total number of elements present in the list variable is 5. So the maximum index number would be 4. By passing 5 to the list variable fruitnames, it fails to find any element that has index number of 5. So it would throw an error.

Note: Before calling any list variable by its index number we must have to sure index number is less than its total number of elements present.

In the above example, we can see the elements of the list variable fruitnames so we can easily count. But in real project environment we may receive millions of element in single list variable, so in that scenario it is not easy to count the total number of elements present in a list variable.

Is there any way, we can find the total number of elements present in a list?

Answer: Yes we can find the total number of elements present in a list. And the solution is to use Python function len(). Len function counts total number of elements present in a list. Inside the len function we have to pass the variable whose element count we want to know.

Syntax

len(desired_list_variable)

Example

print(len(fruitnames))

Output: 5

Therefore the maximum index number would be 4.

So by calling the index number we can print the elements present in a list.

Lets print all the elements present in the list variable fruitnames.

Solution

print(fruitnames[0])

print(fruitnames[1])

print(fruitnames[2])

print(fruitnames[3])

print(fruitnames[4])

Output:

Orange

Banana

Grape

Pine Apple

Jack fruit

With the above simple approach we have printed all the elements present in the list. Now, imagine, what if we have more than one million different types of fruit names in the list variable. In that scenario, the above approach is definitely a time consuming, huge effort consuming process. So there must be some best alternate solution.

The solution is to use for loop. (In one of the previous chapter I had discussed about for loop)

Using for loop we can print all the elements of list variable in two ways.

Solution 1

We know the starting index number of any list is zero. With the help of len function, we can calculate the total number of elements present in a list. So here we have starting point and ending point. Which means we have a range here.

for indexnumber in range(0, len(fruitnames)):

print(fruitnames[indexnumber])

Output

Orange

Banana

Grape

Pine Apple

Jack fruit

See just two line of codes did the job and reduced all efforts. No matter even if billions of elements listed in a list variable the above two line of codes is going to perform the desired requirement.

Solution 2

Using simple in keyword like below

for fruitname in fruitnames:

print(fruitname)

Output

Orange

Banana

Grape

Pine Apple

Jack fruit

In the above approach we have used simple in keyword. The name in between for and in is a reference name. You can use any name you want and the name should not contain space or any special character.