Home > Software engineering >  converting a string into ordered list in python
converting a string into ordered list in python

Time:06-23

top_imdb_movies = ['The Shawshank Redemption', 'The Godfather', 'The Dark Knight']

How would i iterate over top_imdb_movies to display the following ordered list:

1.The Shawshank Redemption
2.The Godfather
3.The Dark Knight

CodePudding user response:

Maybe something like

i=1
for movie in top_imdb_movies:
   print(f"{i}. {movie}"
   i = i 1

CodePudding user response:

top_imdb_movies = ['1. The Shawshank Redemption', '2. The Godfather', '3. The Dark Knight']

for i in top_imbd_movies:
    print(i)

CodePudding user response:

Use enumerate with a start value of 1 as follows:

top_imdb_movies = ['The Shawshank Redemption', 'The Godfather', 'The Dark Knight']

for i, movie in enumerate(top_imdb_movies, 1):
    print(f'{i}. {movie}')

Output:

1. The Shawshank Redemption
2. The Godfather
3. The Dark Knight

CodePudding user response:

top_imdb_movies = ['The Shawshank Redemption', 'The Godfather', 'The Dark Knight']

movie_number=0

for movie_number, movie in enumerate(top_imdb_movies):
    print(f"{movie_number   1}. {movie}")

output

1. The Shawshank Redemption
2. The Godfather
3. The Dark Knight
  • Related