×

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

Operator Overloading in Python

In any programming language, + is an arithmetic operator; it adds two numbers to give the sum. Have you ever tried to concatenate two strings? Concatenating two strings is adding...

5 minutes read.

Python Time Library

We will consider various functions given by the python module library with examples.This python time module helps to work on time in python to get the current time.Before going with...

4 minutes read.

Find key from value in dictionary python

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

5 minutes read.

Python String splitlines() method

Python String splitlines() method The string.splitlines() method in Python splits the specified string and returns a list of the lines in the string, breaking at line boundaries.  Syntax splitlines([keepends]) Parameter keepends(optional): This parameter specifies if...

1 minute read.

Python lock

Before understanding the concept of Python-lock we need to understand what kind if condition we require to implement the locks in Python. Let us start with multithreading and its implementation...

5 minutes read.

How to Practice Python Programming

Learning python kkis a step towards coding. Python gets one closer to programming languages. It is essential to practice every programming language to become a professional in coding. It is...

4 minutes read.

Python String expandtabs() method

Python String expandtabs() method The string.expandtabs() method in Python returns a copy of the string where all tab characters are expanded using spaces. Syntax String.expandtabs([tabsize]) Parameter tabsize: This parameter specifies the number of characters to...

1 minute read.

Python Struct

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....

3 minutes read.

Abstraction in Python

What is meant by abstraction generally? A very general notion of a thing or work is known as abstract. To be more precise, the process of having a brief idea but...

4 minutes read.

Number Pattern Programs in Python

Learning Python is very easy, and it understands in better way compared to other programming languages. One of the many reasons why it has emerged as the most widely used...

4 minutes read.

ComboBox in Python

Python includes several graphical user interface (GUI) libraries, including PyQT, Tkinter, Kivy, WxPython, and PySide. Tkinter is the most commonly used GUI module in Python because it is simple and...

2 minutes read.

Write Text files in Python

Let's discuss writing the text file in detail. Generally, writing text also follows the same procedure as like reading text. It varies only in some specific places. Steps followed for writing...

4 minutes read.

Drop() Function in Python

Drop() Function: The python programming language consists of various libraries; some of them are pandas and matplotlib. Data scientists mainly use the pandas library to analyze the data more easily and...

3 minutes read.

Classes & Objects in Python

Classes and Objects are used in most modern programming languages, mainly in Object-oriented Programming languages. What is meant by Object Oriented Programming language? Usage of objects and their components in order to...

7 minutes read.

Implementing geometric shapes into the game in python

Geometric Drawings We are trying to draw various shapes of geometry by using pygame module to implement Geometric Drawings in game. Let us revise few syntax of basic shapes, to get started...

7 minutes read.

Word frequency Python

Word frequency Python In this tutorial, we will write the Python program to count the occurrence of a word (word frequency) in a given sentence. We will learn all the approaches...

5 minutes read.

How to create a list in Python?

How to create a list in python Python is known for its versatility and its data structures help us to perform different kinds of operations on various datasets irrespective of their...

4 minutes read.

Anaconda in Python 3

What is Anaconda in Python 3? Anaconda is an open-source package combining Python and R programming languages used for data analytics and processing data. It consists of a huge number of...

4 minutes read.

Data Structures and Algorithms Using Python | Part 1

Data Structures: Data Structure is defined as a way to organize and store the data so that we can access the data and work more efficiently. Data structures also describe the...

18 minutes read.

Python Modulo

For basic calculations, Python provides operators. Python supports a broad range of Arithmetic Operators to do arithmetic, as given below: +Addition*Multiplication-Subtraction/Division//Floor division**Exponentiation%Remainder/Modulus As you can see, one of these basic arithmetic operators...

8 minutes read.