Reputation: 2033
I was trying to update the list in other scenario other than using map function. I tried loop and on one of my operation, I get unexpected result. Here is my code.
#my_function_which_is_only_for_printing
def app(l):
for i in l:
print(i)
l=[1,2,'3','4'] #list_with_int_and_str
app(l) #calling_function
#As result my all output are integer
#It Should be integer and character rather then all as integer
My Expected output is like this 1 2 3 4 And I should get it like this 1 2 '3' '4'
Upvotes: 0
Views: 45
Reputation: 2612
Your function is doing what you want: 1 and 2 are of int
type and '3' and '4' are of str
type:
def app(l):
for i in l:
print("{} is: {}".format(i, type(i)))
l = [1,2,'3','4']
app(l)
1 is: <class 'int'>
2 is: <class 'int'>
3 is: <class 'str'>
4 is: <class 'str'>
Edit: To get string representation of the list
elements, like @Paul Panzer suggested in the comments, you could do print(repr(i))
:
def app(l):
for i in l:
print(repr(i), end=' ') # Print on the same line
l = [1,2,'3','4']
app(l)
1 2 '3' '4'
>>>
repr(object) says:
Return a string containing a printable representation of an object.
Upvotes: 1