CYBER WEEK - EXTRA SAVINGS EVENT
TRY A FREE LESSON

Are tuples supposed to be edited?

Screen Link:
https://app.dataquest.io/c/112/m/350/guided-project%3A-profitable-app-profiles-for-the-app-store-and-google-play-markets/10/most-common-apps-by-genre-part-two

My Code:
def display_table(dataset, index):
table = freq_table(dataset, index)
table_display =
for key in table:
key_val_as_tuple = (table[key], key)
table_display.append(key_val_as_tuple)

table_sorted = sorted(table_display, reverse = True)
for entry in table_sorted:
    print(entry[1], ':', entry[0])

I can’t understand the code. Tuples are supposed to be immutable, how can we introduce new tuple? I somehow understood the code till frequency table but this is something beyond my comprehension.
Thanks.

1 Like

I have exactly the same doubt about print code. What i can think of is that since in the tuple, at value is at index 0 and key is at index 1( it is other way round in dictionary), the print is commanding the output in the correct format of (key, value) that is (name of app, percentage )

Read the backgound text on most common apps 2 and 3 to understand why dictionary is converted in “list of tuples” ( and not just tuples). Short answer is sort function can’t be used on dictionary as it will generate only the keys, whereas we want to sort it by values. Using tuples help us sort by values ( that is descending order of percentages)

1 Like

Hello @raturi22 ,

Thanks for replying I am stuck with this project since last month and I simply can’t move ahead once there is a gap in understanding of what has been taught and what has been applied in the project. I was very excited to join the dataquest initially with the feedback based learning and writing our own code but the jupyter notebook seems to have taken that feedback away and there is absolutely no one to help other than a few exchanges from peers on the problem posted, which are not sufficient at times. It feels as if this kind of learning isn’t really suited to the person like myself as it is taking a long long time to even complete basics.

I would like to ask whether there is any whatsapp or telegram or discord group where peers can instantly ask questions and help each other?

Regards,
Ankush

Also, I read Part 2 of most common apps but the basic doubt remains as to why tuples are created when they are supposed to be immutable (we cannot edit and add entities into tuples and the examples I have seen have predefined values in tuples).

Having said this, is there any conceptual gap with my understanding of tuples as a whole. Your insights are welcome.

Thanks,
Ankush Malik

Can you elaborate on this?
Which part of the 3 line code you pasted is introducing a new tuple?
Are you talking about the print() statement?
Print statement can take an infinitely long series of variables or strings to print. The variables will be implicitly casted to strings so even if you send in numbers they will be printed as strings.

Tuple is immutable but the contents can be mutable.

t= ([],[1])
t[0].append(10)
t

Out[4]: ([10], [1])

Even if the contents are not mutable containers like lists, but immutable primitives like int, float, theres nothing stopping someone from unpacking the contents of a tuple and building a new tuple from them, in this question it seems to be doing some reordering of elements.

Here, i want to take this chance to talk about learning and setting up feedback loops for yourself in general.
You mention Tuples are supposed to be immutable. If I guess right, this is something you read from somewhere and took it as true.
My challenge to you is how do you prove this by coding something?
What code and what error tells you a tuple is not mutable? (Making your code break in as many ways as possible maximizes exposure)

What are the ways to unpack a tuple?
What are the ways to construct a tuple?
What python constructs automatically construct a tuple without you specifically writing tuple([1,2]) or (1,2)?

These are all the extra questions to google along the way as you follow Dataquest path.
It’s the same with any education provider. To prevent going off course or out of scope, most things need more explanation. According to my experience with numerous course providers, Dataquest’s explanations are already way more detailed than the others.

Here’s a channel that is comprehensive on the python theory that i enjoy: How variables work in Python | Explained with Animations - YouTube

The “Transferring Learning” Section in my article may jolt some exploratory ideas from you: Reflections from teaching a Data Science Bootcamp

In my article i talk about unpacking key-value pairs and unintuitively using only 1 of them, and the opposite of unpacking a list and adding your own values to create a dictionary. Analogously, this question forces you to play around with various data structures, converting between dictionaries and lists. This is a very common thing to do, and trains creativity. In future you may realize that tuples as keys are not valid json and while tuples can be used as dictionary keys in python, if you tried to convert such dictionaries to json with json.dump, it will break. Then the hack is to convert the 2-element tuple into a single string like {('a','b'):2} becoming {'a_b':2} to make it valid.