Sobes.tech
Junior — Middle

How to output all list elements without using a for loop?

sobes.tech AI

Answer from AI

In Python, you can output all elements of a list without using a for loop, for example, by using the print function and unpacking the list:

my_list = [1, 2, 3, 4, 5]
print(*my_list)

This will print the elements separated by spaces: 1 2 3 4 5.

You can also use the map function together with print:

list(map(print, my_list))

Although map also iterates under the hood, there is no explicit for loop in the code.