Python lists and for in Statements [on hold]


Python lists and for in Statements [on hold]


def print_list(x):
for a in x:
print(x)

c = [1, 2, 4, 5, 6]
print(print_list(c))



Output:


[1, 2, 4, 5, 6]
[1, 2, 4, 5, 6]
[1, 2, 4, 5, 6]
[1, 2, 4, 5, 6]
[1, 2, 4, 5, 6]



In my textbook, it says that the code should simply generate the list however,
in my console the list repeats 5 times and I used my debugger to see why it does that but it still does not illustrate how the output is 5 lines of the list.
Help?



This question appears to be off-topic. The users who voted to close gave this specific reason:





typo, change print(x) to print(a)
– eyllanesc
2 days ago


print(x)


print(a)




5 Answers
5


def print_list(x):
for a in x:
print(a) #print (a),not print(x)



You must print(a), not x to print each item of the list.





Simply a typo.. :) I hope it solved your problem.
– butterfly_princess
2 days ago


def print_list(x):
for a in x:
print(a)
c = [1,2,4,5,6]
print_list(c)



First you are calling print_list function. Inside the print_list, you are iterating list c, which contains 5 values and print c as its. So 5 times you print list C, and comeback print function None to print in the main. Instead of C print a, your problem will solve and it will print values in the list.


print_list


print_list


list c


list C


print


None


def print_list(x):
for a in x:
print(a)#here you have to print a
c = [1, 2, 4, 5, 6]
print_list(c)

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV