List Iteration in Python
In this tutorial, we will learn how to iterate list in Python.
List in Python
- A list is an ordered group of values which includes several kinds of values.
- A list is a mutable container, which means any changes can be made to the list values.
- A finite sequence is defined mathematically and is represented as a python list. The items or components of a list are its values. The same value can be repeated more than once in a list. Each occurrence is regarded as a separate item.
- By using their index, list elements may be retrieved. The first element's index is 0, while the last one's index is -1.
- Square brackets [ ] serve as list separators. A comma character is used to denote the separation of list components.
Example:
n = [1, 2, 3, 4]
Characteristics of Python List
- A list is a form of container sequence that can carry both items of the same type and items of various types, in contrast to flat sequences that can only hold items of one type.
Example:
n = ['one' , 2 , 3.0]
- A list in Python is a structured grouping of things. In reality, if the order in which the items are arranged differs across two lists of the same things, they are not the same.
- Lists are mutable in Python.
Create a List
A dynamically sized array is exactly what a list is. A list is just a group of items separated by commas and contained in brackets ([]). The Python programming language's simplest containers, lists, are a key component. The most potent Python tool is a list since they don't necessarily have to be homogenous. Datatypes such as Integers, Strings, and Objects may all be included in the same list. Lists can be changed even after they are created because they are changeable.
Example:
List = [10, 20]
print(List)
Access List Element
Python lists are numbered and have an established order. A list's indexing is done using 0 as the initial index, and each member is indexed in accordance with a certain order. Each item on the list has a separate spot in the list, making it possible to access items on the list.
- Using the index number will allow us to access the list elements. For access to a list item, use the index operator[]. We need an integer for the index. With nested indexing, nested lists can be accessed.
Example:
List = ["Hello", "Welcome", "Bye"]
print(List[0])
List Iteration
Using Loops:
We can use loops in Python to Iterate Lists.
Syntax:
for variable in list:
Example:
list = [10, 20, 30]
for x in list:
print(x)
Output:
10
20
30
Using range() method
A for loop with the range() function in Python can be used to explore and iterate over a list.
The range() method, which produces or generates a sequence of numbers from the start index supplied up to the end index as indicated in the parameter list, essentially returns a sequence of integers.
Syntax:
range (start, stop[, step])
- start(upper limit): This option specifies the starting integer value or index for the created series of numbers.
- stop(lower limit): This option specifies the end value or index for the resulting sequence of numbers.
- step: It gives the difference between each integer in the to-be-generated sequence.
The range() function creates an integer series starting at the start value and ending at the stop value, but it excludes the end value from the sequence, i.e., the stop number or value, from the final sequence.
Example:
list = [10, 20, 30]
for x in range(len(list)):
print(list[x])
Output:
10
20
30
List Comprehension
Python List Comprehension can determine whether the input is a list, string, tuple, or other types, type of data in order to generate a list of members that have a certain property or specification.
Syntax:
[expression for elements in list]
Example:
list = [10, 20, 30]
[print(x) for x in list]
Output:
10
20
30
Iterate using Python enumerate()
The list can be iterated more efficiently by using the enumerate() function in Python.
The enumerate() method returns an enumerated object after adding a counter to the list or any other iterable.
As a result, it requires less effort to maintain track of the pieces throughout the iteration.
Syntax:
enumerate(iterable, start)
Example:
list = [10, 20, 30]
for x, res in enumerate(list):
print (x,":",res)
Output:
0 : 10
1 : 20
2 : 30