Initialization and accessing the elements are two activities associated with working on a list in Python, one of the computer languages. Simply speaking, initialization refers to the assignment of an initial value for a variable or data object. Furthermore, it is possible to access the items of a list in Python.
Initialization of a Python List
Sometimes one may have a need to carry out the initialization of a list in advance so as to have a particular number of elements. Furtherore, initialization can certainly prove helpful in accessing the elements.
#!/usr/bin/env python
n1 = [0 for i in range(15)]
Moreover, n2 = [0] * 15
print(n1)
print(n2)
Also, n1[0:10] = [10] * 10
print(n1)
In this example, initialization of two lists takes place using a list comprehension and a * operator.
Also, n1 = [0 for i in range(15)]
Furthermore, n2 = [0] * 15
These two lists are initialized to fifteen zeros.
n1[0:10] = [10] * 10
First ten values are replaced with 10s.
$ ./initialization.py
As such, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Moreover, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Most noteworthy, [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 0, 0, 0, 0, 0]
Browse more Topics under Lists
- Creating Lists
- Traversing Lists
- Appending Lists
- Updating and Deleting Elements
- Composition
- Lists as Arguments
- List Functions and Methods
Accessing the Elements of Python List
For accessing the elements, one must make use of the index operator [ ]. Furthermore, an important point to keep in mind while accessing the elements is that the index must be an integer. Also, the access of nested lists can take place by making use of nested indexing.
# Python program to demonstrate
# accessing of element from list
# Creating a List with
# the use of multiple values
List = ["Toppr", "For", "Toppr"]
# accessing a element from the
# list using index number
print("Accessing a element from the list")
print(List[0])
print(List[2])
# Creating a Multi-Dimensional List
# (By Nesting a list inside a List)
List = [['Toppr', 'For'] , ['Toppr']]
# accessing an element from the
# Multi-Dimensional List using
# index number
print("Acessing a element from a Multi-Dimensional list")
print(List[0][1])
print(List[1][0])
Output
Accessing a element from the list
Toppr
Toppr
Acessing a element from a Multi-Dimensional list
For
Toppr
Negative Indexing
In Python, negative sequence indexes show the positions from the array’s end. Rather than to carry out a computation of the offset as in List[len(List)-3], it would be sufficient to just write List[-3]. Moreover, negative indexing means beginning from the end, -1 represents the last item, -2 represents the second-last item, etc.
List = [1, 2, 'Geeks', 4, 'For', 6, 'Geeks']
Furthermore, # accessing a element using
Moreover, # negative indexing
Also, print("Accessing element using negative indexing")
# print the last element of list
Also, print(List[-1])
Furthermore,# print the third last element of list
print(List[-3])
Output:
Accessing element using negative indexing
Toppr
For
FAQs For Initializing and Accessing the Elements
Question 1: Briefly explain how one can access the elements of a list in Python?
Answer 1: Python has a great built-in list type that is called “list”. Furthermore, List literals are within square brackets [ ]. Most noteworthy, one would have to make use of the len() function and square brackets [ ] in order to access data, with the first element at index 0.
Question 2: What is meant by the initialization in computer science?
Answer 2: In computer science, initialization refers to the assignment of an initial value for a data object or variable. Furthermore, the manner in which the performance of the initialization takes place is dependent on the programming language. Moreover, the initialization also depends on factors like an object’s type, storage class, etc.
Leave a Reply