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

Question:

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?

Asked By: Lazy_Kitty

||

Answers:

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.

Answered By: CoolGuy

#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

Answered By: shahrad hekmati
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.