-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.py
58 lines (54 loc) · 1.79 KB
/
list.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Filter List Using Comprehension
# Easy
# Problem Description
# Create a program to create a list of odd numbers from a list of numbers using list comprehension.
#
# 1. Create a list with the following data items 12, 17, 28, 19, 11, and assign it to the numbers variable.
# Create a new list and only print 17, 19, 11 (odd numbers) using list comprehension.
# Print newly created list.
# # Replace ___ with your code
# numbers = [12, 17, 28, 19, 11]
# # Use list comprehension to get only odd numbers from the numbers list
# newlist = [i for i in numbers if i % 2 != 0]
#
# # print new list
# print(newlist)
#
# 2 Natural Numbers List
# Easy
# Problem Description
# Create a program to create a list of first n natural numbers using list comprehension.
#
# Take an integer input from the user and assign it to n.
# Use list comprehension to create a list with items from 1 to n.
# Print the list.
# Assumption: We will assume that the user will always enter a positive integer.
# Replace ___ with your code
# get integer input for variable n
# n = int(input())
#
# # create the list using list comprehension
# numbers = [i for i in range(1,n+1)]
#
# # print the list
# print(numbers)
#
#
#
# 3.Create a Dictionary
# Easy
# Problem Description
# Create a program to create a dictionary using dictionary comprehension.
#
# Create a list with the following data items 1, 2, 3, 4, and assign it to the numbers variable.
# Create a new dictionary using comprehension where the keys are items of the list, and their corresponding values are equal to key+1.
# Print the dictionary.
# Replace ___ with your code
#
# numbers = [1, 2, 3, 4]
#
# # create the dictionary using comprehension
# dictionary={key:key+1 for key in numbers}
#
# # print the dictionary
# print(dictionary)