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 г. 6: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.

Комментарии

Только авторизованные пользователи могут публиковать комментарии.
Пожалуйста, авторизуйтесь или зарегистрируйтесь