Learn › Class 11 › Computer Science / Informatics Practices › Lists, tuples, dictionaries
Lists, tuples and dictionaries
Example 1: Count runs scored by each batter
Open one step at a time. Try the next step yourself first.
- Step 1. runs = {}
Show step 2
for name, r in [('Virat', 40), ('Rohit', 25), ('Virat', 12)]: runs[name] = runs.get(name, 0) + r
Show step 3
runs is now {'Virat': 52, 'Rohit': 25}.
Example 2: Slicing a list
Open one step at a time. Try the next step yourself first.
- Step 1. L = [10, 20, 30, 40, 50]
Show step 2
L[1:4] is [20, 30, 40].
Show step 3
L[::-1] is [50, 40, 30, 20, 10], the list reversed.