[beginner ask] What's difference?

Hello Everyone,
I’m new in Python and I’ve a doubt over the below code, Why this codes produces different results:

from csv import reader
open_file = open('googleplaystore.csv')
android = reader(open_file)

unique_app = []
duplicated_app = []
for app in android:
    name = app[0]
    if name in unique_app:
        duplicated_app.append(name)
    else:
        unique_app.append(name)
print('Uniques apps: ', len(unique_app,), '\n')
print('Duplicated apps: ', len(duplicated_app), '\n'\
     'Exemple duplicated app: ', duplicated_app[:15])
with open('googleplaystore.csv') as android:
    unique_app = []
    duplicated_app = []
    for app in android:
        name = app[0]
        if name in unique_app:
            duplicated_app.append(name)
        else:
            unique_app.append(name)
    
print('Uniques apps: ', len(unique_app,), '\n')
print('Duplicated apps: ', len(duplicated_app), '\n'\
     'Exemple duplicated app: ', duplicated_app[:15])

I appreciate your time and tips
Regardiness

Hey, Diego.

Basically android is different in each code snippet which in turn makes it so that app is different.

In the first code snippet, each app is a list that looks like what you expect it to look like. In the second code snippet, each app is a string that represents the whole row.

So, for instance, in the last iteration app is:

  • In the first code snippet: ['iHoroscope - 2018 Daily Horoscope & Astrology', 'LIFESTYLE', '4.5', '398307', '19M', '10,000,000+', 'Free', '0', 'Everyone', 'Lifestyle', 'July 25, 2018', 'Varies with device', 'Varies with device']
  • In the second code snippet: 'iHoroscope - 2018 Daily Horoscope & Astrology,LIFESTYLE,4.5,398307,19M,"10,000,000+",Free,0,Everyone,Lifestyle,"July 25, 2018",Varies with device,Varies with device'.

I hope this helps.

1 Like

Glad for your help Bruno.
I got your point, but if you can, tell me a thing, the return of reader() (frist snippet) and open using with (seccond snippet), shouldn’t the same? It means an object iterable?
oficial csv docummentation
What can I do to get a list of list using with … open…?

Again, Thank you so much :smiley:

You’re welcome, Diego.

It being an iterable, doesn’t mean it is the same thing. For instance, both lists and strings are iterables and one wouldn’t say they are the same thing.

To understand what’s going on, please checkout the corresponding mission in the Data Engineer path, specifically from screen 7 onward. If you understand those screens, you should be able to find solutions different from the one I suggest below.

A quick solution is as follows.

from csv import reader
with open('googleplaystore.csv') as android:
    android = reader(android)
    unique_app = []
    duplicated_app = []
    for app in android:
        name = app[0]
        if name in unique_app:
            duplicated_app.append(name)
        else:
            unique_app.append(name)
    
print('Uniques apps: ', len(unique_app,), '\n')
print('Duplicated apps: ', len(duplicated_app), '\n'\
     'Exemple duplicated app: ', duplicated_app[:15])
1 Like

Many thanks, Bruno.
I appreciate answers with path to learn, I don’t known I can use reader(…) inside a with open…
I’m feeling stupid, but ok!

Thank you again :clap:

1 Like