Unexpected error in 316-6 (Functions Intermediate)

In exercise 6 of ‘Functions Intermediate’ I submitted my answer below:

Paste your code here
```# INITIAL CODE
def open_dataset(file_name='AppleStore.csv', header=True):        
    opened_file = open(file_name)
    from csv import reader
    read_file = reader(opened_file)
    data = list(read_file)
    
    if header:
        return data[1:], data[:1]
    else:
        return data
    
all_data = open_dataset()

apps_data = all_data[0]
header = all_data[1]

What I expected to happen:
I expected the tuple to contain the header and the rest of the data set and my answer to be submitted correctly.

What actually happened:
My answer couldn’t be submitted correctly. Instead, it showed me this:


- actual + expected

- [['id',
-   'track_name',
-   'size_bytes',
-   'currency',
-   'price',
-   'rating_count_tot',
-   'rating_count_ver',
-   'user_rating',
-   'user_rating_ver',
-   'ver',
-   'cont_rating',
-   'prime_genre',
-   'sup_devices.num',
-   'ipadSc_urls.num',
-   'lang.num',
-   'vpp_lic']]
+ ['id',
+  'track_name',
+  'size_bytes',
+  'currency',
+  'price',
+  'rating_count_tot',
+  'rating_count_ver',
+  'user_rating',
+  'user_rating_ver',
+  'ver',
+  'cont_rating',
+  'prime_genre',
+  'sup_devices.num',
+  'ipadSc_urls.num',
+  'lang.num',
+  'vpp_lic']

If I change
return data[1:], data[:1]
to
return data[1:], data[0]
it works fine although it’s the same result in my point of view.

Any comments?

Here’ something i wrote about “sameness” Replacing value in list of lists after looping. (just the 1st block is enough)

Maybe you can see what is different after that. Basically, data[:1] is not data[0]. Eg. '2' is not 2
You can also see the mistake from the answer checker output too. Pink contents are not the same as Blue contents.
Finally you can prove to yourself in 1 minute by creating your own small list and indexing into it.
Opening jupyter is too slow:

  1. Open anaconda prompt
  2. Type ipython

Hello @hanqi,

Thanks for your quick response!
I can see now that the data[:1] resulted in a list of list ("[[…,…,]]") whereas data[0] is actually a list ("[…,…,…]").

Is that what you mean?

Yes good spot.
It matters when coding to think about how data is represented (https://docs.python.org/3/howto/regex.html#the-backslash-plague), it’s encoding (if doing string/byte manipulations), what collections/data structures it lives in (this informs what methods are available for use). Such a skill of understanding data types become more important once you start moving away from core python structures that you build yourself to structures designed by others, like series/dataframes in pandas.
Don’t just depend on visual debugging. What you see may not be what the program represents it as, so type() is a lifesaver learning new libraries. However it’s still limited in alerting you to difference between list vs list of list or dict vs dict of dict, so requires firstly on your part a strong awareness of how the data is stored (what level of nesting is there?) and during processing to index into intermediate data structures to ensure they are what you think.

Okay, thank you for your explanation! :blush: