Said Py
13 квітня 2020 р. 19:10

Як перевірити список порожній у python

Привіт, хлопці, сьогодні ми дізнаємось, як перевірити, чи список порожній


#empty list
list_1 = []

#checking if list is empty by using len()
if not llist_1:
    print('yes! the list is empty.')

#output 
#yes! the list is empty.    

По статті запитували0питання

4

Вам це подобається? Поділіться в соціальних мережах!

Анатолий Жучков
  • 13 квітня 2020 р. 19:25
#empty list
list_1 = []
#ноль, пустой кортеж, пустой список, пустая строка принимаются как False
if not list_1:
    print('yes! the list is empty.')

ИГ
  • 14 квітня 2020 р. 11:12
  • (відредаговано)

Автор, очень плохой совет, не делайте так никогда.

делайте как написал Анатолий Жучков, в коментариях

Said Py
  • 15 квітня 2020 р. 06:16

thanks
but if i wanna to check if list is exists how can i do that?

Evgenii Legotckoi
  • 15 квітня 2020 р. 13:11

It works similar. If list not exists, then list is None

if list_1:
    print('yes! the list is not None')

Therefore the following code is right

list_1 = None

if not list_1:
    print('the list is empty or None')
Evgenii Legotckoi
  • 15 квітня 2020 р. 13:16

And just FYI

I am admin of this site, and I want ask you add special separator in your articles, like this

___

This is special separator, which separate preview part from main part of article. Please, use it in articles (notes) on this site.

I think you saw some changes in articles after first publication. Just I made moderation and some cleaning of article for improvement of content.

Коментарі

Only authorized users can post comments.
Please, Log in or Sign up