Use for Loop That Loops Over a Sequence in Python
Last Updated :
24 Feb, 2023
In this article, we are going to discuss how for loop is used to iterate over a sequence in Python.
Python programming is very simple as it provides various methods and keywords that help programmers to implement the logic of code in fewer lines. Using for loop we can iterate a sequence of elements over an iterable like a tuple, list, dictionary, set, String, etc. A sequence consists of multiple items and this item can be iterated using in keyword and range keyword in for loop.
Syntax of for loop:
for variable in Sequence:
#....code...
Syntax of range method
- range(a) -> Generates a sequence of items from 0th index to a-1 index.
- range(a, b) -> Generates a sequence of items from ath index to b-1 index.
- range(a, b, step) -> Generates a sequence started from a, end at b-1 and c is difference.
Note: Range method is not used when any type of sequence won't support indexing.
Example 1: Python For Loop using List
A list is used to store the multiple values of the different types under a single variable. It is mutable i.e., items in the list can be changed after creation. The list is created by enclosing the data items with square brackets.
Python3
# list creation
li = ["Geeks", "for", "Geeks"]
for i in li:
print(i)
print('-----')
for i in range(len(li)):
print(li[i])
OutputGeeks
for
Geeks
-----
Geeks
for
Geeks
Time complexity: O(n) where n is the length of the list.
Auxiliary space: O(1) as no extra space is used.
Example 2: Python For Loop using Tuple
A tuple is used to store the multiple values of the different types under a single variable. It is immutable i.e., items in a tuple cannot be changed after creation. A tuple is created by enclosing the data items with round brackets.
Python3
# tuple creation
seq = ("Geeks", "for", "Geeks",
"GFG", "Learning", "Portal")
for i in seq:
print(i)
print('-----')
for i in range(3, len(seq)):
print(seq[i])
OutputGeeks
for
Geeks
GFG
Learning
Portal
-----
GFG
Learning
Portal
Time complexity: O(n), where n is the length of the tuple.
Auxiliary space: O(1), as we are not using any additional data structure to store the elements of the tuple.
Example 3: Python For Loop using Dictionary
Dictionary is an unordered collection of items where data is stored in key-value pairs. Unlike other data types like list, set and tuple it holds data as key: value pair. for loop uses in keyword to iterate over each value in a dictionary.
Python3
# dictionary creation
dic = {1: "a", 2: "b", 3: "c", 4: "d"}
for i in dic:
print(i)
Example 4: Python For Loop using Set
A set is an unordered, unindexed, and immutable datatype that holds multiple values under a single variable. It can be created by surrounding the data items around flower braces or a set method. As the set is unindexed range method is not used.
Python3
# set creation
setSeq = {"unordered", "unindexed", "immutable"}
for i in setSeq:
print(i)
Outputunordered
unindexed
immutable
Example 5: Python For Loop using String
Here we passed the step variable as 2 in the range method. So we got alternate characters in a string.
Python3
# string creation
str = "GFG Learning-Portal"
for i in str:
print(i, end="")
print()
for i in range(0, len(str), 2):
print(str[i], end="_")
OutputGFG Learning-Portal
G_G_L_a_n_n_-_o_t_l_
Similar Reads
Loops in Python - For, While and Nested Loops Loops in Python are used to repeat actions efficiently. The main types are For loops (counting through items) and While loops (based on conditions). Additionally, Nested Loops allow looping within loops for more complex tasks. While all the ways provide similar basic functionality, they differ in th
9 min read
Print first m multiples of n without using any loop in Python While loops are the traditional way to print first m multiples of n, Python provides other efficient methods to perform this task without explicitly using loops. This article will explore different approaches to Printing first m multiples of n without using any loop in Python.Using List Comprehensio
3 min read
Sequence and Series in Python Sequences and series are fundamental concepts in mathematics. A Sequence is an ordered list of numbers following a specific pattern, while a series is the sum of the elements of a sequence. This tutorial will cover arithmetic sequences, geometric sequences, and how to work with them in Python. Arith
5 min read
Specifying the increment in for-loops in Python Let us see how to control the increment in for-loops in Python. We can do this by using the range() function. range() function range() allows the user to generate a series of numbers within a given range. Depending on how many arguments the user is passing to the function, the user can decide where
2 min read
How to Replace Values in a List in Python? Replacing values in a list in Python can be done by accessing specific indexes and using loops. In this article, we are going to see how to replace the value in a List using Python. We can replace values in the list in several ways. The simplest way to replace values in a list in Python is by using
2 min read
Output of Python Programs | Set 23 (String in loops) Prerequisite: Loops and String Note: Output of all these programs is tested on Python3 1. What is the output of the following? Python3 my_string = "geeksforgeeks" i = "i" while i in my_string: print(i, end =" ") None geeksforgeeks i i i i i i ⦠g e e k s f o r g e e k s
2 min read
How to Access Index using for Loop - Python When iterating through a list, string, or array in Python, it's often useful to access both the element and its index at the same time. Python offers several simple ways to achieve this within a for loop. In this article, we'll explore different methods to access indices while looping over a sequenc
2 min read
Difference between for loop and while loop in Python In this article, we will learn about the difference between for loop and a while loop in Python. In Python, there are two types of loops available which are 'for loop' and 'while loop'. The loop is a set of statements that are used to execute a set of statements more than one time. For example, if w
4 min read
Loops in R (for, while, repeat) Loops are fundamental constructs in programming that allow repetitive execution of code blocks. In R loops are primarily used for iterating over elements of a vector, performing calculations and automating repetitive tasks. In this article we will learn about different types of loops in R.1. For Loo
6 min read
Iterate over a set in Python The goal is to iterate over a set in Python. Since sets are unordered, the order of elements may vary each time you iterate. You can use a for loop to access and process each element, but the sequence may change with each execution. Let's explore different ways to iterate over a set.Using for loopWh
2 min read