-2

What's the difference between this:

Genres=[ 'rock', 'R&B', 'Soundtrack', 'R&B', 'soul', 'pop']
for Genres in Genres:
    print(Genres)

and this:

Genres=[ 'rock', 'R&B', 'Soundtrack', 'R&B', 'soul', 'pop']
for genres in Genres:
    print(genres)

In the second, I define second variable, but for what? Answer is the same, what's the difference?

I really can't understand, can you explain me is there some benefits or disadvantages in defining second variable

Abdul Aziz Barkat
  • 19,475
  • 3
  • 20
  • 33
MrFrey
  • 1
  • 2
  • 1
    Just `print(Genres)` at the end of each code snippet and see the difference. Have also a look at the PEP 8 style guide for Python, especially the part about capitalization of variable names. – Thierry Lathuille May 29 '23 at 15:48

1 Answers1

0

I'll start explaining how this code works and then we can look at the other one.

Genres=[ 'rock', 'R&B', 'Soundtrack', 'R&B', 'soul', 'pop']
for genres in Genres:
    print(genres)

At the first iteration, you assign the first item of Genres ('rock') to the variable genres, then you print it. In the second iteration, you assign the second item of Genres ('R&B') to the variable genres, then you print it. This is repeated for the lenght of the list (with every item). When you get to the end of the list, the for loop ends.

Let's now look at the other code


Genres=[ 'rock', 'R&B', 'Soundtrack', 'R&B', 'soul', 'pop']
for Genres in Genres:
    print(Genres)

Here, you're taking the variable Genres and reasigning it. Now in each iteration it will be the next item in the list. Python is intelligent and has the original list "stored", so it can keep iterating, but if we do after this code print(Genres), It will return 'pop', while the other code will return the whole list.

You should be careful when you reasign variables in python, because you will lose access to the information that was there before.

The difference is that in the first code, you create a new variable that changes every loop, genres, and change that variable, while in the second, you destroy the list to assign the reference of Genres to the corresponding item in the list.

To check it, you can compare

Genres=[ 'rock', 'R&B', 'Soundtrack', 'R&B', 'soul', 'pop']
for genres in Genres:
    print(genres)
print(Genres)

which will print each genre and then the full list,

Compared to

Genres=[ 'rock', 'R&B', 'Soundtrack', 'R&B', 'soul', 'pop']
for Genres in Genres:
    print(Genres)
print(Genres)

that will print each genre and then the last one again

  • 1
    This is a good explanation, but there is a little more to "in the second, you destroy the list". While this will be true in the end in this specific case, making a name no longer refer to an object doesn't delete it. The deletion only happens if there isn't *any* reference left to the object, in which case it will get garbage collected – Thierry Lathuille May 29 '23 at 17:00
  • Yeah, you don't really destroy it, you just aren't able to access it any more – David Siret Marqués May 30 '23 at 11:01