Python Bytes: List Comprehensions Introduction

Опубликовано: 14 Май 2021
на канале: CyberScribe.org
34
1

Python Bytes
Intermediate Python Topics
List Comprehensions Introduction

#A method of making lists from other values.

#Defining a list and its contents at the same time.

new_list = [ item for item in iterable ]

#An 'iterable' is an object that contains other objects within it.
#Lists, Dictionaries, Tuples, Sets, range(), and more.

#You can work backwards from a loop to create the comprehension and understand the logic.
#This is a good technique when you are still new to list comprehensions.

#Example 1:
new_list = list()
for num in range(0,10):
new_list.append(num)

new_list = [num for num in range(0,10)]


#Example 2:
fqdns = ['abc.dev.com', 'dew.qka.com', 'kwlan.ewa.com']
hostnames = list()
for fqdn in fqdns:
hostnames.append(fqdn.split('.')[0])

hostnames2 = [ fqdn.split('.')[0] for fqdn in fqdns ]


Смотрите видео Python Bytes: List Comprehensions Introduction онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь CyberScribe.org 14 Май 2021, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 34 раз и оно понравилось 1 людям.