Skip to main content

Python For Loop

for loop are used to iterates block of statements in the certain situation. that is useful for iterate statement of dictionary,list,tuples sets and string. In this post we are learn about how to use for loop in various situations.

for example execute looping statement in decided number of times.

for value in range(0,7):
  print(value)
Output
0
1
2
3
4
5
6

range() function is returning the number of sequence, based on provided parameters. first is an optional parameter by default its start with zero. if we are given first parameter value then range is start of first parameter and second parameter value are set last value. for example.

for value in range(10,15):
  print(value)
Output
10
11
12
13
14

range() can be used multiple situation.there is one more type are available. that are accept three parameter range(start,end-1, increment-size). for example.

#start index=1
#end point 50-1=49
#increment value by 5
for index in range(1,50,5):
  print(index)
Output
1
6
11
16
21
26
31
36
41
46

Example of for loop

#create a list 
listData=[1,2,3,4]

for value in listData:
  #display value of list item
  print(value) 
Output
1
2
3
4

Using of this approach we can access string, tuples, set elements.

print("List Elements")
for value in [100,200,300]:
  print(value)

print("String Elements")
for strText in "PYTHON":
  print(strText)

print("Set Elements")
for setData in {1,2,4,5,6}:
  print(setData)
Output
List Elements
100
200
300
String Elements
P
Y
T
H
O
N
Set Elements
1
2
4
5
6

Access Dictionary Elements

Dictionary contain unordered element. we can display dictionary element using for loop in following way.

dictData={"k1":"v1","k2":"v2","k3":"v3"}
for key in dictData:
  #display key  value pair
  print(key,dictData[key]) 
Output
k3 v3
k2 v2
k1 v1

In this example key is located dictionary index value. and help of index we can access the value of dictionary elements (dictData[key]). There is another alternative are available to get key and value of dictionary element.

dictData={"k1":"v1","k2":"v2","k3":"v3"}
for key,value in dictData.items():
  #display key value pair
  print(key,value)

this example are very simple. but assume that the dictionary elements exist combination of multiple data using sets,tuples, and other sets type. so how split this dictionary data using for loop. for example.

data={"1":[1,2,3],
   "2":{"c","java","c++"},
   "3":{"k1":100,"k2":200,"k3":300}}

Note that this dictionary is combination of multiple python data. here given one solution.

Python multiple dict Example
def display(data):
  print(type(data),"Items")
  if(type(data)==dict):
    for key,value in data.items():
      if(type(value)==list or 
        type(value)==tuple or 
        type(value)==dict or 
        type(value)==set or 
        type(value)==dict ):
        display(value) #not simple data
      else: 
        print(key,value)
  else:
    for key in data:
      print(key)
  
data={"1":[1,2,3],
   "2":{"c","java","c++"},
   "3":{"k1":100,"k2":200,"k3":300}}
display(data)
Output
<class 'dict'> Items
<class 'list'> Items
1
2
3
<class 'dict'> Items
k2 200
k3 300
k1 100
<class 'set'> Items
c++
c
java

This program is capable to control all type of situations when dictionary data structure is not predefined.





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment