You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"Given the nested list **data** = [[['a', 'b', 'c'], ['d', 'e', 'f']], [['g', 'h', 'i'], ['j', 'k', 'l']]], using list comprehension, **create a new list containing all vowels from data.**\n",
23
+
"\n",
24
+
"**Output:**\n",
25
+
"\n",
26
+
"For the given data, the output list should be **['a', 'e', 'i'].**"
"# looping through sub-lists using list comprehension\n",
53
+
"# sub-list in list\n",
54
+
"# sub-sub-list in sub-list\n",
55
+
"# sub-sub-sub-list in sub-sub-list\n",
56
+
"\n",
57
+
"vowel_list = [x for ele_list in data for coll_list in ele_list for x in coll_list if x in ['a', 'e', 'i','o','u']]\n",
58
+
"\n",
59
+
"print(\"Vowels present in given data are: \", vowel_list)"
60
+
]
61
+
},
62
+
{
63
+
"cell_type": "markdown",
64
+
"id": "1bf9e7f4-885e-4f16-8e73-1edc83028db6",
65
+
"metadata": {
66
+
"id": "1bf9e7f4-885e-4f16-8e73-1edc83028db6"
67
+
},
68
+
"source": [
69
+
" # Question 2:\n",
70
+
"\n",
71
+
"\n",
72
+
"Given the nested tuple **info** = ((('Alice', 25), ('Bob', 30)), (('Charlie', 22), ('David', 28))), using tuple unpacking and list comprehension, create a new list containing the **ages of all individuals whose names start with 'D'**.\n",
73
+
"\n",
74
+
"**Output:**\n",
75
+
"\n",
76
+
"For the given info, the output list should be [28,26,32,27]."
"# looping through sub-tuples using list comprehension\n",
108
+
"# sub-tuple in tuple\n",
109
+
"# (name,age) in sub-tuple\n",
110
+
"# if name.startswith('D') --> append age into list\n",
111
+
"age_list_whose_name_startswith_letter_D = [age for x in info for (name,age) in x if name.startswith(\"D\")]\n",
112
+
"print(age_list_whose_name_startswith_letter_D)"
113
+
]
114
+
},
115
+
{
116
+
"cell_type": "markdown",
117
+
"id": "07ac759c-f8ac-4779-ba86-0a0a2cea5a6e",
118
+
"metadata": {
119
+
"id": "07ac759c-f8ac-4779-ba86-0a0a2cea5a6e"
120
+
},
121
+
"source": [
122
+
"# Question 3\n",
123
+
"\n",
124
+
"Given the nested dictionary **data** = {'A': {'x': [1, 2, 3]}, 'B': {'y': [4, 5, 6]}}, using dictionary comprehension, create a new dictionary where each key is from the original dictionary and each value is the sum of its associated list values.\n",
125
+
"\n",
126
+
"**Output:**\n",
127
+
"\n",
128
+
"For the given data, the output dictionary should be {'A': 6, 'B': 15}.\n",
"consonant_in_data = [x for collections in data for items in collections for x in items if x not in ['a', 'e', 'i', 'o', 'u']]\n",
93
+
"\n",
94
+
"print(consonant_in_data)"
95
+
]
96
+
},
97
+
{
98
+
"cell_type": "markdown",
99
+
"id": "07ac759c-f8ac-4779-ba86-0a0a2cea5a6e",
100
+
"metadata": {
101
+
"id": "07ac759c-f8ac-4779-ba86-0a0a2cea5a6e"
102
+
},
103
+
"source": [
104
+
"# Question 3\n",
105
+
"\n",
106
+
"Given the nested dictionary **data** = {'A': {'x': [1, 2, 3]}, 'B': {'y': [4, 5, 6]}}, using dictionary comprehension, create a new dictionary where each key is from the original dictionary and each value is the sum of its associated list values.\n",
107
+
"\n",
108
+
"**Output:**\n",
109
+
"\n",
110
+
"For the given data, the output dictionary should be {'A': 6, 'B': 15}.\n",
0 commit comments