I have a simple function inside which i run a loop and then perform split operation on the extracted string element. Once operation is done on the current element i am printing the 2nd splitted text of the complete string and then moving to next element of the list.
However, when the function is called, i find the output printed in reverse order i.e. from the last element of list to first.
Here is the code:
JavaScript
x
6
1
s={"i am good", " i am fine"}
2
def splitter():
3
for i in s:
4
print(i.split()[2])
5
splitter()
6
Output:
JavaScript
1
3
1
fine
2
good
3
However, expected output is:
JavaScript
1
3
1
good
2
fine
3
Advertisement
Answer
Because sets are not ordered in python, you can use list instead
JavaScript
1
6
1
s = ["i am good", " i am fine"]
2
def splitter():
3
for i in s:
4
print(i.split()[2])
5
splitter()
6