Home > Net >  Python List Question: Which part of code is more logical?
Python List Question: Which part of code is more logical?

Time:01-20

list_of_names = ['Vladimir', 'Victor', 'Alexander', 'Darius', 'Dimitriy']

#1
for list in list_of_names:
  print(list)
# 2
for list in range(len(list_of_names)):
  print(list_of_names[list])

Which part(#1 or #2) is better to announce value? Which will be correct?

CodePudding user response:

You are clearly using less functions and code for the first loop and they both return the same thing. The second code is also more clear and easer to read so no one will get confused.

CodePudding user response:

#1 Is suitable for showing the list elements in general #2 Is good for printing either all of the elements or setting limits for starting point, ending point, and steps. ==> range(start, end, steps)

If you want to print all of the elements of the list use #1 and if you want to print certain elements range or in specific steps use #2

  • Related