In: Computer Science
Describe how tuples can be useful with loops over lists and dictionaries, and give Python code examples. Create your own code examples. Do not copy them from the textbook or any other source.
Your descriptions and examples should include the following: the zip function, the enumerate function, and the items method.
It is very easy question i tried to make it very simple and explaining in subpart,so please read carefully.
---------------------------------------------------------------------------------------------------------------------------------------------
-In a python we know that enumerate(),items() both return
tuples.
-we are know that is tupple is faster than list in case of
iterating the element.
--**To iterate the List**--
-In a following example we try to iterate the items in the
list.
ex1:
for item in Item_list:
print(item)
-But in the above example ex1,we just print all element of the
list Item_list but when we
want to find position of any element in the list ,in that case we
have to of keep
track of index number of all element in the List.look ex2.
ex2:
pos = 0
for item in Item_list:
print( pos, item)
pos +=1
-Here we can get or print position of any element in the list.But
instead of this way we
also do this task using enumerate() function.
-This function takes a any collection and returns it an enumerate
object.
-Best use of this function is get a need to keep a count of
iterations.
-It also adds a counter to an iterable and returns it in a form of
enumerate object.
-This object can then be used directly in for loops.Now see
ex3,
ex3:
for item in enumerate(Item_list):
print (item)
-here we directly return enumerate object i.e we not need to
keep track of each position ,and
this object return a tuple with index of each element.But hold on
we dont want tupple
we just need to print element with their position then look
following ex.
ex4:
for pos, item in
enumerate(Item_list):
print( pos, item)
- Here we did the same task that we did in ex2,bu diffrent way
using enumerate() function.
with reduce number of line.
-enumerate() function adds automatic count of each item that is
fetched and by default it
starting from zero.
--**To iterate the Dictionary**--
-Normally when we need to iterate a dictionary with key and
value we do following way,
ex5:
for key in Item_dict:
print(key,
Item_dict[key])
-But iterating the dictionary in this way which is
not good efficient way.
-so we use items() method is used to iterating dictionary with key
and value.
-It is generates a sequence of tuples where the first element is a
dictionary key, and the
2nd element is the value for that key.look ex6
ex6:
for key, value in Item_dict.items():
print(key, value)
-Python zip() function The function takes in iterables as
arguments and returns an iterator.
-It returns a zip object, which is an iterator of tuples where the
first and second item which passed iterator make a paired
together.
-zip() can iterate any of lists, tuples, dictionaries, sets.
ex7:
t = zip(tupple1, tupple2)
print(tuple(x))
-In the ex7 we passed 2 tupple to zip() and print zip object
t.
-In a output it display the tupple containing paired tupples i.e
tupple of tupples.