Sayantan's Blog On Python Programming

ACCESSING PYTHON LISTS

ACCESSING PYTHON LISTS

ORDER OF PYTHON LISTS

Order in Python list is very important. Because list elements are accessed in the order they appear in the list.

# List order example

str_list = ['Python', 'Java', 'C++', 'C', 'XML', 'PHP']

print("\nElement of list str_list ->", str_list)

ACCESSING PYTHON LISTS : Output

ACCESSING PYTHON LISTS : Output

In the above example, we can see that the list elements are printer in the same order as they appear in the list.

LIST INDEX

Indexes are used to access the individual element in the list. Index in Python starts with 0.

# Accessing List elements example

str_list = ['Python', 'Java', 'C++', 'C', 'XML', 'PHP']

print("Element of list str_list ->", str_list)

print("1st element of list str_list ->", str_list[0])
print("2nd element of list str_list ->", str_list[1])
print("3rd element of list str_list ->", str_list[2])
print("4th element of list str_list ->", str_list[3])
print("5th element of list str_list ->", str_list[4])
print("6th element of list str_list ->", str_list[5])

ACCESSING PYTHON LISTS : Output

Element of list str_list -> ['Python', 'Java', 'C++', 'C', 'XML', 'PHP']
1st element of list str_list -> Python
2nd element of list str_list -> Java
3rd element of list str_list -> C++
4th element of list str_list -> C
5th element of list str_list -> XML
6th element of list str_list -> PHP

In the above example, we can see that in list str_list there are 6 elements. To print the first element we have used the index 0. Then subsequently increasing the index has provided the next elements in the list.

If we try to access an element which does not exists then Python will raise an error -> “list index out of range

# Accessing List elements exception example

str_list = ['Python', 'Java', 'C++', 'C', 'XML', 'PHP']

print("Element of list str_list ->", str_list)

print("1st element of list str_list ->", str_list[0])
print("2nd element of list str_list ->", str_list[1])
print("3rd element of list str_list ->", str_list[2])
print("4th element of list str_list ->", str_list[3])
print("5th element of list str_list ->", str_list[4])
print("6th element of list str_list ->", str_list[5])

print("7th element of list str_list ->", str_list[6])

ACCESSING PYTHON LISTS : Output

Element of list str_list -> ['Python', 'Java', 'C++', 'C', 'XML', 'PHP']
1st element of list str_list -> Python
2nd element of list str_list -> Java
3rd element of list str_list -> C++
4th element of list str_list -> C
5th element of list str_list -> XML
6th element of list str_list -> PHP
Traceback (most recent call last):
  File "d:\PYTHON\PROGRAM\tempCodeRunnerFile.python", line 19, in <module>
    print("7th element of list str_list ->", str_list[6])
                                             ~~~~~~~~^^^
IndexError: list index out of range

In the above example, we can see that up to index position 5 Python has printed all the list elements. But when we try to print the 7th element at index position 6 which does not exists, Python has raised the index out of range error.

ACCESSING LIST INDEX IN REVERSE ORDER

Python allows negative values as index which in turn helps us to access the list elements in reverse order.
When we are accessing the list elements from reverse order then index position 0 will not be used as we are using negative index.

# Accessing List elements in reverse order example

str_list = ['Python', 'Java', 'C++', 'C', 'XML', 'PHP']

print("Element of list str_list ->", str_list)

print("1st element of list str_list from the end ->", str_list[-6])
print("2nd element of list str_list from the end ->", str_list[-5])
print("3rd element of list str_list from the end ->", str_list[-4])
print("4th element of list str_list from the end ->", str_list[-3])
print("5th element of list str_list from the end ->", str_list[-2])
print("6th element of list str_list from the end ->", str_list[-1])

ACCESSING PYTHON LISTS : Output

Element of list str_list -> ['Python', 'Java', 'C++', 'C', 'XML', 'PHP']
1st element of list str_list from the end -> Python
2nd element of list str_list from the end -> Java
3rd element of list str_list from the end -> C++
4th element of list str_list from the end -> C
5th element of list str_list from the end -> XML
6th element of list str_list from the end -> PHP

In the above example, we can see that by using negative index values we have printed the list in normal order. In below example we will see that if we start with index value -1 then the entire list will be printed in reverse order.

# Accessing List elements in reverse order example

str_list = ['Python', 'Java', 'C++', 'C', 'XML', 'PHP']

print("Element of list str_list ->", str_list)

print("1st element of list str_list from the end ->", str_list[-1])
print("2nd element of list str_list from the end ->", str_list[-2])
print("3rd element of list str_list from the end ->", str_list[-3])
print("4th element of list str_list from the end ->", str_list[-4])
print("5th element of list str_list from the end ->", str_list[-5])
print("6th element of list str_list from the end ->", str_list[-6])

ACCESSING PYTHON LISTS : Output

Element of list str_list -> ['Python', 'Java', 'C++', 'C', 'XML', 'PHP']
1st element of list str_list from the end -> PHP
2nd element of list str_list from the end -> XML
3rd element of list str_list from the end -> C
4th element of list str_list from the end -> C++
5th element of list str_list from the end -> Java
6th element of list str_list from the end -> Python

COUNTING LIST ELEMENTS

len() FUNCTION

len() function helps us to count the number elements present in the list.

# Counting List elements with len() function example

str_list = ['Python', 'Java', 'C++', 'C', 'XML', 'PHP']

print("\nElement of list str_list ->", str_list)

print("\nNumber of element present in the list str_list ->", len(str_list))

ACCESSING PYTHON LISTS : Output

Element of list str_list -> ['Python', 'Java', 'C++', 'C', 'XML', 'PHP']

Number of element present in the list str_list -> 6

In the above example, we can see that len() function returns 6 as we have created the list of 6 elements.

count() FUNCTION

# Checking the existence of a List elements with count() function example 1

str_list = ['Python', 'Java', 'C++', 'C', 'XML', 'PHP']

print("\nElement of list str_list ->", str_list)

print("\nNumber of occurrence of Java in list str_list ->", str_list.count('Java'))

ACCESSING PYTHON LISTS : Output

Element of list str_list -> ['Python', 'Java', 'C++', 'C', 'XML', 'PHP']

Number of occurrence of Java in list str_list -> 1

Now if we create duplicate elements for Java then count function will consider the duplicates as well.

# Checking the existence of a List elements with count() function example 2
 
str_list = ['Python', 'Java', 'C++', 'C', 'Java', 'XML', 'Java']
 
print("\nElement of list str_list ->", str_list)
 
print("\nNumber of occurrence of Java in list str_list ->", str_list.count('Java'))

ACCESSING PYTHON LISTS : Output

Element of list str_list -> ['Python', 'Java', 'C++', 'C', 'Java', 'XML', 'Java']

Number of occurrence of Java in list str_list -> 3

We can see that, the Java element is occurred thrice in the list that’s why the count returns the result as 3.

# Checking the existence of a List elements with count() function example 3
  
int_list = [4, 2, 6, 10, 4, 9, 2, 6, 6]
  
print("\nElement of list int_list ->", int_list)
  
print("\nNumber of occurrence of 2 in list int_list ->", int_list.count(2))
print("\nNumber of occurrence of 4 in list int_list ->", int_list.count(4))
print("\nNumber of occurrence of 6 in list int_list ->", int_list.count(6))

ACCESSING PYTHON LISTS : Output

Element of list int_list -> [4, 2, 6, 10, 4, 9, 2, 6, 6]

Number of occurrence of 2 in list int_list -> 2

Number of occurrence of 4 in list int_list -> 2

Number of occurrence of 6 in list int_list -> 3

In the above example, we can see that the element 2 and 4 are occurred twice and 6 is occurred thrice. So, the count() function returned the data accordingly.

The count() function returns 0 if any element is not found in the list.

# Checking the existence of a List elements with count() function example 4
  
int_list = [4, 2, 6, 10, 4, 9]
  
print("\nElement of list int_list ->", int_list)  
print("\nNumber of occurrence of 12 in list int_list ->", int_list.count(12))

str_list = ['Python', 'Java', 'C++', 'C', 'Java', 'XML']
 
print("\nElement of list str_list ->", str_list) 
print("\nNumber of occurrence of PHP in list str_list ->", str_list.count('PHP'))

ACCESSING PYTHON LISTS : Output

Element of list int_list -> [4, 2, 6, 10, 4, 9]

Number of occurrence of 12 in list int_list -> 0

Element of list str_list -> ['Python', 'Java', 'C++', 'C', 'Java', 'XML']

Number of occurrence of PHP in list str_list -> 0

In the above example, we can see that 12 is not present in the list int_list. So count() returned 0. Similarly, the element PHP is not present in the list str_list. So, the count() function is returned 0.

set() FUNCTION

set() function removes the duplicate elements from the list.

In the previous example, we have seen that the Java element was taken thrice in the list. We can remove the duplicate Java element by using set() function.

# Removing duplicate elements of a List with set() function example 1
 
str_list = ['Python', 'Java', 'C++', 'C', 'Java', 'XML', 'Java']
 
print("\nElement of list str_list ->", str_list)
 
print("\nAfter removing the duplicates in list str_list ->", set(str_list))

ACCESSING PYTHON LISTS : Output

Element of list str_list -> ['Python', 'Java', 'C++', 'C', 'Java', 'XML', 'Java']

After removing the duplicates in list str_list -> {'Java', 'C', 'C++', 'XML', 'Python'}

In the above output, we can see that only one Java element is visible. The duplicates are removed.

# Removing duplicate elements of a List with set() function example 2
 
int_list = [4, 2, 6, 10, 4, 9, 2, 6]
 
print("\nElement of list int_list ->", int_list)
 
print("\nAfter removing the duplicates in list int_list ->", set(int_list))

ACCESSING PYTHON LISTS : Output

Element of list int_list -> [4, 2, 6, 10, 4, 9, 2, 6]

After removing the duplicates in list int_list -> {2, 4, 6, 9, 10}

In the above example, we can see that the element 4, 2, 6 are added twice in the list. But after using set() function the duplicate elements are removed.

RELATED TOPICS:

6 thoughts on “ACCESSING PYTHON LISTS”

  1. Pingback: INTRODUCTION TO PYTHON LISTS - Sayantan's Blog On Python Programming

  2. Pingback: LIST SORTING OPERATIONS - Sayantan's Blog On Python Programming

  3. Pingback: REMOVING DUPLICATES USING PYTHON SET - Sayantan's Blog On Python Programming

  4. Pingback: PYTHON LIST BUILT-IN FUNCTIONS - Sayantan's Blog On Python Programming

  5. Pingback: PYTHON LIST DEEP COPY - Sayantan's Blog On Python Programming

  6. Pingback: INTRODUCTION TO PYTHON SETS - Sayantan's Blog On Python Programming

Leave a Comment

Your email address will not be published. Required fields are marked *