My QA Projects

QA Projects I was involded.

View on GitHub

Dictionary iteration

Iteration by keys

Syntax

value = dictionary[key]

Example1

d={'b':2,'a':1,'c':3,}
for key in d.keys():
    value=d[key]
    print(f"d['{key}']={value}")
# Output
d['b']=2
d['a']=1
d['c']=3

Example2

d={'b':2,'a':1,'c':3,}
for key in sorted(d.keys()):
    value=d[key]
    print(f"d['{key}']={value}")
# Output
d['a']=1
d['b']=2
d['c']=3

Iteration by values

Example1

d={'b':2,'a':1,'c':3}
total = 0
for value in d.values():
    total += value
print(total)
# Output
# 6

Iteration by items

for key, value in [(key1, value1), 
                    (key2, value2), 
                    (key3, value3), ]

Example

    d={'a':1,'b':2,'c':3}
    for key,value in d.items():
        print(f"d['{key}']={value}")
# Output
#   
expression value
key,value (‘a’,1)
d.items() [(‘a’:1),(‘b’:2),(‘c’:3)]

WHY

Dictionary Comprehension

Syntax

{key: vlaue for item in iterable if cond_exp}

(he is presenting is for the sake of completion, but he doesn’t feel it is very useful per se since it complex nature)

fruits=['apple', 'banana', 'cherry', 'durian']
d={fruit[0].lower(): fruite.capitalization() for fruit in fruits}
print(d)

# Output
# {'a': 'Apple', 'b':'Banana', 'c':'Cherry', 'd':'Durian'}