×

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

Related Topics

Python Command line Arguments

Python Command line Arguments The command-line argument is used to the change functionality of the program. It's an extra command the programmer can use while launching a program. These commands have many uses...

7 minutes read.

Python hex() function

Python hex() function The hex() function in Python  converts the specified number into a hexadecimal value. Syntax hex(x) Parameter x: The parameter represents an Integer value. Return This function Returns hexadecimal string. Example 1 # Python Program explaining # the hex() function print("The...

1 minute read.

Python Marshmallow

Python:  Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It is said...

3 minutes read.

Insertion Sort using Python

Insertion sort is a type of sorting technique that is used for sorting an array with random elements. Using sorting methods, any unsorted array can be sorted into ascending or...

3 minutes read.

AX Contour in Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

Python NewLine

In python, a new line character denoted by "\n" is known as an escape character or escape sequence, and it will force the cursor to change its position to the next...

6 minutes read.

Self in Python

 “self" is neither a keyword nor has a special meaning in Python, but it has a place and a job to do in Object-oriented programming. When we create a class...

6 minutes read.

Python Not Equal Operator

Python provides us with many operators to make tasks easier. There are about 7 categories of operators in Python. One of the 7 classifications is the comparison operators. Just as...

3 minutes read.

Python Modules List

Python is like an ocean. If you have been working with Python, you might’ve already heard the word module. When we solve a problem in mathematics, there will be several...

3 minutes read.

Python Assert

Python Assert Python provides an assert statement which is used to check the logical expression. If the given logical expression is true, then it precedes for the next line; otherwise, it raises an...

2 minutes read.

Python String upper() method

Python String upper() method The string.upper() method in Python returns a copy of the string converted to uppercase. Syntax string.upper() Parameter NA Return This method returns a copy of the string converted to uppercase. Example 1 # Python...

1 minute read.

How to reverse a string in Python

In Python language, we have a few ways to reverse a string. In this article, let us discuss these ways and understand the suitability of these ways in different scenarios...

5 minutes read.

Sys Module in Python

What are Modules? The Modules are the kind of files that contain Python statements and definitions. The module is known by the name of the file followed by the suffix “.py”....

5 minutes read.

Python Selectors

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

iobase Python

All I/O flow classes derive from this conceptual base class. Derived classes will have to execute several of the class's abstract data types. The loop method is supported by all members of...

4 minutes read.

Python Read CSV file

The CSV stands for “comma-separated value,” which is defined as a simple file format that is used to store data into a tabular form such as a database or spreadsheet. It is...

7 minutes read.

How to Add Elements in List in Python?

How to Add Elements in List in Python We all are aware of the wide spectrum of applications where the data structures of Python are used. Be it lists, tuples, or...

4 minutes read.

Python String ljust() method

Python String ljust() method The string.ljust() method in Python will left align the string, where the padding is done using the parameter fillchar (space is default). Syntax String.ljust(width[, fillchar]) Parameter width: This parameter represents the...

2 minutes read.

Python | a += b is not always a = a + b

a += b in Python doesn't always behave the same way as a = a + b; the same operands can produce different outcomes depending on the circumstances. But we...

3 minutes read.