Home » Python

Accessing a list and its elements in Python

Here, we are going to learn about the various methods to access the complete list and its elements based on the index in Python programming language.
Submitted by IncludeHelp, on March 05, 2020

Accessing a list

To access a list – we can simply print the list object and the complete list prints as an output.

    print (list_object)

Example:

In this example, we will declare and assign the list, will print their types, and will print the list. To find the type of an object – we use type() method.

# declaring lists
list1 = [10, 20, 30, 40, 50, 10, 60, 10]
list2 = ["Hello", "IncludeHelp"]
list3 = ["Hello", 10, 20, "IncludeHelp"]

# printing the list and its elements
print("list1: ", list1)
print("list2: ", list2)
print("list3: ", list3)

# printing the types
print("Type of list1 object: ", type(list1))
print("Type of list2 object: ", type(list2))
print("Type of list3 object: ", type(list3))

Output

list1:  [10, 20, 30, 40, 50, 10, 60, 10]
list2:  ['Hello', 'IncludeHelp']
list3:  ['Hello', 10, 20, 'IncludeHelp']
Type of list1 object:  <class 'list'>
Type of list2 object:  <class 'list'>
Type of list3 object:  <class 'list'>

Accessing list elements based on the index

To access list elements based on the given index – we simply pass the index starting from 0 to length-1 to access the particular element and we can also pass the negative index to access the list elements in the reverse order (-1 to access the last element, -2 to access the second last element, and so on...)

Syntax:

    list_object[index]

Example:

# declaring lists
list1 = [10, 20, 30, 40, 50]

# Accessing the elements of a list by its index
print("list1[0]: ", list1[0])
print("list1[1]: ", list1[1])
print("list1[2]: ", list1[2])
print("list1[3]: ", list1[3])
print("list1[4]: ", list1[4])
print() # prints a new line 

# Accessing the elements of a list by its index
# in reverse order

print("list1[-1]: ", list1[-1])
print("list1[-2]: ", list1[-2])
print("list1[-3]: ", list1[-3])
print("list1[-4]: ", list1[-4])
print("list1[-5]: ", list1[-5])

Output

list1[0]:  10
list1[1]:  20
list1[2]:  30
list1[3]:  40
list1[4]:  50

list1[-1]:  50
list1[-2]:  40
list1[-3]:  30
list1[-4]:  20
list1[-5]:  10

Access elements using List slicing

We can also access a set of elements by using list slicing by defining the start_index and end_index.

Syntax:

    list_object[[start]:[end])

Note: One of the values start or end may optional – consider the below example.

Example:

# declaring lists
list1 = [10, 20, 30, 40, 50]

# printing list
print("list1: ", list1)

# printing elements using list slicing

# prints 5 elements from starting
print("list1[:5]: ", list1[:5])
# prints 3 elements from starting
print("list1[:3]: ", list1[:3])

# prints all elements from the index 0
print("list1[0:]: ", list1[0:])
# prints all elements from the index 3
print("list1[3:]: ", list1[3:])

# prints the elements between index 2 to 3
print("list1[2:3]: ", list1[2:3])
# prints the elements between index 0 to 4
print("list1[0:4]: ", list1[0:4])
# prints the elements between index 1 to 4
print("list1[1:4]: ", list1[1:4])

# prints elements in the reverse order
print("list1[ : : -1]: ", list1[ : : -1])

Output

list1:  [10, 20, 30, 40, 50]
list1[:5]:  [10, 20, 30, 40, 50]
list1[:3]:  [10, 20, 30]
list1[0:]:  [10, 20, 30, 40, 50]
list1[3:]:  [40, 50]
list1[2:3]:  [30]
list1[0:4]:  [10, 20, 30, 40]
list1[1:4]:  [20, 30, 40]
list1[ : : -1]:  [50, 40, 30, 20, 10]


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.