How would you generally traverse any Python data structure? Or in plain english , how do you visit each element in a Python data structure? Don't know? ... Guess what .. Use a "for loop". Now, if you can use a "for loop" to traverse through a Python data structure, that data structure is called an Iterable .. Simple!! Let's create a Python list
lst = [1,2,3,4]
for element in lst:
print(element)
---> 1
2
3
4
Now let's create a Python set
set_new = set([4,5,6,7])
for element in set_new:
print(element)
---> 4
5
6
7
Let's create a Python string now
strng = 'Python'
for element in strng:
print(element)
---> 'P'
'y'
't'
'h'
'o'
'n'
If you try a similar approach with dictionaries and tuples, the effect will be the same. We call Python data structures like lists, tuples, set, string, dictionary as Iterables. More on this in the next part
Comments
Post a Comment