Use for
You mean using for loop to iterate it? I can’t get each of the element in the list by indexing it?
You can, but you need all of them to have the same size. using a for you don't assume anything about the list size.
Ah no wonder as one of the list just contain 1 element in it… Thank you so much!
Yes, precisely.
Can we use if and length? if len(v) >= 2: print(v[2]) else: print(v[1])
why? what if the list is empty? if it has 3 items, 4?
What if we first check if the length of list is greater than 1 then run a for loop to print the list
You can check the length of the list, but you can also use list comprehension for printing things too
Yes but that syntax looks difficult to read for me
Depending on what you want/need, it's the way to write consistent code. But I agree the syntax is weird.
Обсуждают сегодня