Techniques of Looping

Techniques of Looping

 Python Looping Strategies

loops python, python while, python while loop, python do while, python foreach, python while true, range function python

Python supports a variety of booster techniques with specific component functions, from a variety of sequential containers. These methods are basically verya useful for competitive systems and for various projects that require a specific method with loops that maintain the entire code structure.


Where are they used?


Different planning techniques are most effective in areas where we do not need to manage the structure and layout of the container as a whole, instead only printing items for a single use model, no spatial changes occur in the container. This can also apply to time-saving situations.


The various overhaul techniques using Python data structures are:


Uses enumerate ()

enumerate () is used to extract containers that print a reference number and the value of that particular index.

Lets see by an example:

for key, value in enumerate(['The', 'Best', 'site', 'for' , 'coding']): 

    print(key, value) 


Output:

0 The

1 Best

2 site

3 for

4 coding

Zip ()

zip () is used to combine 2 identical containers (list-list or list-dict) to print the values   in sequence. The loop is only available until the end of the small container. A detailed description of zip () and enumerate () can be found here.

Lets see this by a code:

devices = ['brand', 'model', 'price']

descriptions = ['hp', '15bs', '45000']


for device, description in zip(devices,descriptions):

print('What is the device {0}? description: {1}.'.format(device, description))


Output :

What is the device brand?  description: hp.

What is the device model?  description: 15bs.

What is the device price?  description: 45000.


Uses iteritem ():

iteritems () is used to extract a dictionary by printing a key value-value pair in a dictionary sequence.

It uses the elements (): 

objects () that perform the same function in a dictionary as iteritems () but have some problems compared to iteritems ().

  • Most of the time. Calling it a big dictionary takes a lot of time.
  • It takes a lot of memory. Sometimes it takes twice as much memory as it is called in a dictionary.


Lets see by an Example:


dict = { "code" : "for", "only" : "dudle" }  

print ("The key value pairs after using items is : ") 

for i,j in dict.items(): 

    print (i,j)


Output:

The key value pairs after using items is : 

code for

only dudle


Note: iteritems () removed from python 3.x only works for python 2.x.Instead use dict.items ().

Used for sorting (): edited () used for printing the container in the ordered order. It does not filter the container, but just print the container set for 1.The use of a set () can be combined to extract duplicate events.

 Example

l = [ 1 , 3, 5, 5, 6, 2, 9, 1, 3 ]

# using sorted() to print the list in sorted order

print ("The list are in sorted order : ")

for i in sorted(l) :

print (i,end=" ")

print ("\r")

# sorting without the duplicates

print ("The list are in sorted order (without duplicates) are : ")

for i in sorted(set(l)) :

print (i,end=" ")


Output:


The list are in sorted order  : 

1 1 2 3 3 5 5 6 9 

The list are in sorted order (without duplicates) are : 

1 2 3 5 6 9 




Using Reversed()

 

Reversed () used to print container prices on a modified order.

Note: Does not show any changes to the actual list

The beauty of using the above techniques above, while wrapping


l = [ 1 , 3, 6, 2, 1, 3 ]

print ("The list is in reversed order : ")

for i in reversed(l) :

print (i,end=" ") 


Output:


The list is in reversed order : 

3 1 2 6 3 1 



Advantage:

  • These processes are faster to implement and reduce coding effort. because, while the timber needs to be built the entire container structure.
  • These Dressing methods do not require any formal structural changes. They have bright words that give the purpose of use. While, no prediction or speculation can be made, while the loop i.e. not easily understood the purpose just by looking.


Recommended Post:


Post a Comment

0 Comments