Comprehension Practice Questions - ( DS140823 ) - 21-09-2023

  1. Create a dictionary comprehension that generates a dictionary containing the squares of numbers from 1 to 5.
    Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

  2. Create a dictionary comprehension that converts a list of names into a dictionary where the keys are the names and the values are the lengths of the names.

Input  : names = ["Alice", "Bob", "Charlie", "David"]
Expected Output : {'Alice': 5, 'Bob': 3, 'Charlie': 7, 'David': 5}
2 Likes
# Create a dictionary comprehension that generates a dictionary containing the squares of numbers from 1 to 5.
# Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

dict_comp = {i:(i**2) for i in range(1,6)}
print(dict_comp)
# Create a dictionary comprehension that converts a list of names into a dictionary where the keys are the names and the values are the lengths of the names.

# Input  : names = ["Alice", "Bob", "Charlie", "David"]
# Expected Output : {'Alice': 5, 'Bob': 3, 'Charlie': 7, 'David': 5}

names = ["Alice", "Bob", "Charlie", "David"]
dict_comp = {names[i] : len(names[i]) for i in range(len(names))}
print(dict_comp)
1 Like
1)
key =[1,2,3,4,5]
value =[1,2,3,4,5]
dic ={key[i]:value[i]**2 for i in range(len(key))}
print(dic)

2)

key =["Alice", "Bob", "Charlie", "David"]
value =["Alice", "Bob", "Charlie", "David"]
dic ={key[i]:len(value[i]) for i in range(len(key))}
print(dic)
1 Like
# 1. Create a dictionary comprehension that generates a dictionary containing the squares of numbers from 1 to 5.
# Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

### 1st way ----------

keys = [1,2,3,4,5]
values = [1,4,9,16,25]
dict_com = {keys[i]:values[i] for i in range(len(keys))}
print("Output: ",dict_com)


### 2nd way -------

keys = [1,2,3,4,5]
dict_com = {keys[i]:keys[i]**2 for i in range(len(keys))}
print("Output: ",dict_com)


### 3rd way -------

dict_com = {i:i**2 for i in range(1,6)}
print("Output: ",dict_com)


# 2. Create a dictionary comprehension that converts a list of names into a dictionary where the keys are the names and the values are the lengths of the names.
# Input  : names = ["Alice", "Bob", "Charlie", "David"]
# Expected Output : {'Alice': 5, 'Bob': 3, 'Charlie': 7, 'David': 5}


names = ["Alice", "Bob", "Charlie", "David"]
dict_com = {names[i]:len(names[i]) for i in range(len(names))}
print("Output: ",dict_com)
1 Like

#Create a dictionary comprehension that generates a dictionary containing the squares of numbers from 1 to 5


key=[1,2,3,4,5]
dict_com={key[i]:(i+1)**2 for i in range(len(key))}
print(dict_com)

output:-
{1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

1 Like
# 1. Create a dictionary comprehension that converts a list of names into a dictionary where the keys are the names and the values are the lengths of the names.
names=["Alice","Bob","Charlie","David"]

values=["Alice","Bob","Charlie","David"]

dict_com={names[i]:len(values[i]) for i in range(len(names))}

print(dict_com)
output:-
{'Alice': 5, 'Bob': 3, 'Charlie': 7, 'David': 5}
1 Like

Ans 1-

dict = {i:(i**2) for i in range (1,6)}
print(dict)

Ans 2-

lst = ["Alice","Bob","Charlie","David"]

dict = {lst[i]:len(lst[i]) for i in range(len(lst))}

print(dict)
1 Like
# 1} Create a dictionary comprehension that generates a dictionary 
#    containing the squares of numbers from 1 to 5.

#    Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

square_dict = {i: i**2 for i in range(1, 6)}
print(square_dict)

# 2} Create a dictionary comprehension that converts a list of names into a dictionary 
#    where the keys are the names and the values are the lengths of the names.

#    Input  : names = ["Alice", "Bob", "Charlie", "David"]
#    Expected Output : {'Alice': 5, 'Bob': 3, 'Charlie': 7, 'David': 5}

keys = ['Alice', 'Bob', 'Charlie' , 'David']
my_dict = {value: len(value) for value in keys}
print(my_dict)
1 Like

1:
lst=[1,2,3,4,5]

square_lst={i:i**2 for i in range(1,6)}

print(square_lst)

2:

names = [“Alice”, “Bob”, “Charlie”, “David”]

count_names={names[i]:len(names[i] )for i in range (len(names))}

print(count_names)

# Create a dictionary comprehension that generates a dictionary containing the squares of numbers from 1 to 5.

# Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

dic={x:x**2 for x in range (1,6)}

print(dic)

# Create a dictionary comprehension that converts a list of names into a dictionary where the keys are the names and the values are the lengths of the names.

# Input : names = ["Alice", "Bob", "Charlie", "David"]

# Expected Output : {'Alice': 5, 'Bob': 3, 'Charlie': 7, 'David': 5}

names = ["Alice", "Bob", "Charlie", "David"]

dict={names : len(names) for names in names }

print(dict)Preformatted text
1 Like
  1. Create a dictionary comprehension that generates a dictionary containing the squares of numbers from 1 to 5.
    Expected Output : {1: 1, 2: 4, 3: 9, 4: 16, 5: 25}
dict1={i:i**2 for i in range(1,6)}
print(dict1)
  1. Create a dictionary comprehension that converts a list of names into a dictionary where the keys are the names and the values are the lengths of the names.
names = ["Alice", "Bob", "Charlie", "David"]
dict1 = {i:len(i) for i in names}
print(dict1)
1 Like

key = [1,2,3,4,5]
dict_demo = {i:i**2 for i in key }
print(dict_demo)


names = ["Alice", "Bob", "Charlie", "David"]
dict_names = {i:len(i) for i in names}
print(dict_names)

1 Like