Skip to content

Instantly share code, notes, and snippets.

@hodunov
Created November 7, 2021 12:17
Show Gist options
  • Save hodunov/46a51935dd2d4281211d3a83361e4614 to your computer and use it in GitHub Desktop.
Save hodunov/46a51935dd2d4281211d3a83361e4614 to your computer and use it in GitHub Desktop.
Homework 5 - lists
# list of dictionaries search
text_list = [
{'number': 1, 'notes': 'ToDO1'},
{'number': 2, 'notes': 'ToDO2'},
{'number': 3, 'notes': 'ToDO3'},
{'number': 4, 'notes': 'ToDO4'},
{'number': 5, 'notes': 'ToDO5'},
{'number': 6, 'notes': 'ToDO6'},
]
search = 5
result = next((hw for hw in text_list if hw['number'] == search), None)
# print(result)
# Домашнее задание списки # 5
# 1. Напишите программу, которая удаляет дубликаты элементов
# из списка.
example_list = [1, 2, 3, 1, 2, 5, 6, 7, 8]
clear_list = list(set(example_list))
# or using dict:
mylist = ["a", "b", "a", "c", "c"]
mylist = list(dict.fromkeys(mylist))
# 2. Напишите программу, которая копирует список
# You can use the builtin list.copy() method (available since Python 3.3):
old_list = ["a", "b", "a", "c", "c"]
new_list = old_list.copy()
# You can slice it:
new_list = old_list[:]
# You can use the built in list() function:
new_list = list(old_list)
# If the list contains objects and you want to copy them as well, use generic copy.deepcopy():
import copy
new_list = copy.deepcopy(old_list)
# 3. Напишите программу, которая находит разницу между двумя
# списками и сохраняет ее в новый список. Вывести результат
# на экран
list1 = [33, 2, 4]
list2 = [4, 5, 55]
list_difference = []
for item in list1:
if item not in list2:
list_difference.append(item)
list1 = [33, 2, 4]
list2 = [4, 5, 55]
list_difference = [item for item in list1 if item not in list2]
print(list_difference)
list1 = [33, 2, 4]
list2 = [4, 5, 55]
set_difference = set(list1) - set(list2)
list_difference = list(set_difference)
print(list_difference)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment