Untitled

 avatar
unknown
python
2 months ago
2.5 kB
6
Indexable
#task 1

'''friends = {
    "alice": 21,
    "tom": 26,
    "richer": 20
}

for k, v in friends.items():
    print(f"{k} is {v} years old")'''

#task 2: Given a dictionary, write a function that returns only the keys as a list.

person = {"tom": 18, "alice": 18, "lydia": 18, "scott": 20}

'''def get_key(val):
    res = []
    for k in val.keys():
        res.append(k)
    
    return res

print(get_key(person))'''

#task 3. Given a dictionary, write a function that returns only the values as a list.

person = {"tom": 18, "alice": 18, "lydia": 18, "scott": 20}

'''def get_val(val: dict) -> list:
    res = []
    for v in val.values():
        res.append(v)
    return res

print(get_val(person))'''

#task 4. Write a function that takes two dictionaries and merges them into one.

'''def unity(val1, val2):
    return val1 | val2

person = {"alice": 25, "tom": 45}
info = {"life": "hayat", "coding": "translate"}

print(unity(person, info))'''

#task 5. Write a function that takes a dictionary and returns the key with the highest value.

'''def higher_val(val: dict):
    max_key = val[k]
    for k, v in val.items():
        pass
    '''
    
#task 6. Write a function that takes a string and counts how many times each character appears.

'''def counter(s: str)-> dict:
    res = {}
    
    for i in s:
        if i not in res:
            res[i]+=1
        else:
            res[i] = 1
    
    return res

print(counter("sezamaksuaksusssso"))'''

#task 7. Write a function that takes a dictionary and removes all keys where the value is 0.

'''def remover(val: dict):
    
    for k, v in val.items():
        if v == 0:
            val.pop(k)
        
    print(val) 

print(remover({"name": 0, "infp": 4, "book": 0, "know": 1}))'''

#task 8. Write a function that swaps keys and values in a dictionary.

'''def swapper(val: dict):
    res = {k: v for v, k in val.items()}
    return res

print(swapper({"name": "alice", "age": 18}))'''

#task 9. Write a function that takes a list of dictionaries (each has "name" and "score") and returns the name of the person with the highest score.

'''def myfunc(val: list):
    for items in val:
        for k, v in items.items():
            

ml = [{"alice": 2}, {"tom": 1}, {"angel": 4}, {"adam": 5}]
'''

#task 10. Write a function that takes two lists — one of keys, one of values — and combines them into a dictionary.

def combiner(val1: list, val2: list):
    res = dict(zip(val1, val2))
    return res

res = combiner(["alice", "jane", "mary"], [15, 16, 17])
print(res)
Editor is loading...
Leave a Comment