Skip to content

Commit

Permalink
Update python-quiz.md (#7065)
Browse files Browse the repository at this point in the history
Fixed a logical flaw in Question 178 and also added explanation for the same.  Earlier the intent of the code was ambiguous and the use of list( ) function was not considered.
  • Loading branch information
r-vik authored Oct 20, 2024
1 parent 8af752f commit 57d4cb0
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions python/python-quiz.md
Original file line number Diff line number Diff line change
Expand Up @@ -2593,25 +2593,21 @@ The first loop runs for `m` times and the inner loop will run for `n` times. The
- [x] 2
- [ ] 16

#### Q178. How would you create a list of tuples matching these lists of characters and actors?
#### Q178. Let the lists 'characters' and 'actors' be defined as given. Which of the following lines of code gives the desired output?

```python
characters = ["Iron Man", "Spider Man", "Captain America"]
actors = ["Downey", "Holland", "Evans"]

#example output : [("Iron Man", "Downey), ("Spider Man", "Holland"), ("Captain America", "Evans")]
#Desired output : [("Iron Man", "Downey), ("Spider Man", "Holland"), ("Captain America", "Evans")]
```

- [x] zip (characters, actors)
- [ ] {x:y for x in characters for y in actors}
- [ ] [(x,y) for x in characters for y in actors]
- [ ] ­
- [ ] print( zip( characters, actors ) )
- [ ] print( { x: y for x in characters for y in actors } )
- [ ] print( [ ( x, y ) for x in characters for y in actors ] )
- [x] print( list( zip( characters, actors ) ) )

```
d = {}
for x in range(1, len(characters)):
d[x] = actors [x]
```
Explanation: zip() is the correct function for this usecase. However, zip() makes a zip type object which is an iterator. Therefore, using list() is necessary to convert the zip object into a list of tuples that can be displayed or accessed directly. The other options have logical errors.

#### Q179. When is the `if __name__ == "__main__":` block executed in a Python script?

Expand Down

0 comments on commit 57d4cb0

Please sign in to comment.