Intermediate Python3 - (*)unpacking operator/args

Hey codecademy, I just started the new Intermediate Python3 course and noticed something I would like some clarification for regarding syntax. Does it matter if the third argument(s) passed to the dictionary are one comma-separated string or multiple comma-separated strings. I initially overlooked that your argument (line 5) for ‘order_items’ (‘Orange Juice, Apple Juice’) is one string, so when I inout my data I provided ‘order_items’ as (‘Steak’, ‘Seabass’, ‘Wine Bottle’)), or 3 separate comma-separated strings. My code ran without errors. I also input the order_items as (‘Steak, Seabass, Wine Bottle’)) and got the same result. Are both syntax acceptable? and why? Thanks.

tables = {
1: {
‘name’: ‘Jiho’,
‘vip_status’: False,
‘order’: ‘Orange Juice, Apple Juice’ //Codecademy provided a single comma separated string
},
2: {},
3: {},
4: {},
5: {},
6: {},
7: {},
}
print(tables)

def assign_table(table_number, name, vip_status=False):
tables[table_number][‘name’] = name
tables[table_number][‘vip_status’] = vip_status
tables[table_number][‘order’] = ‘’

Write your code below:

def assign_and_print_order(table_number, *order_items):
tables[table_number][‘order’] = order_items
for item in order_items:
return order_items[:]

assign_table(2, ‘Arwa’, True)
print()

assign_and_print_order(2, ‘Steak’, ‘Seabass’, ‘Wine Bottle’) //I passed 3 comma-separated strings
print(tables)

assign_table(3, ‘Mizzy’, True)
print()

assign_and_print_order(3, ‘Scallops’, ‘Asparagus’, ‘3 Wine Bottles’) //Again, I passed 3 comma-separated strings
print(tables)

1 Like

It does and doesn’t matter at the same time if that makes sense. So the reason it doesn’t matter is specific to this exercise, as all you need to do is assign the order to the order property, and that’s exactly what you are doing. However depending on how the arguments are provided these are not the same objects.

In this example, lets look at the two options. Here is option 1:

assign_and_print_order(2, 'Steak', 'Seabass', 'Wine Bottle')
# results in 'order': ('Steak', 'Seabass', 'Wine Bottle')

Here we can see that the resulting value for order is a tuple composed of 3 values, the 3 values you entered. This is because each input is taken as an individual argument and so fed into a tuple individually.

Here is the second method:

assign_and_print_order(2, 'Steak, Seabass, Wine Bottle')
# results in 'order': ('Steak, Seabass, Wine Bottle',)

We can see here that it is a tuple once again, however there is only one value, that of the full string. This is because you have only provided one argument, and so when unpacked there only is one argument to add as the value.

Which you use in future depends on what it’s used for. For example if you want to access each part of the order individually, number 2 would be more difficult to do this with as you would need to use regex to split up the string whereas number 1 would allow you to just select individual tuple elements.
However if you simply want to output a string to show an order, then number 1 would require manipulation to get to that single string, whilst number 2 is already one string ready to be inserted into a webpage for example.

Hopefully that explains the differences well enough, any other questions do ask!

1 Like

If you can edit your existing post or add an additional post with a link to the project and formatted code, see FAQ:How to ask good questions (and get good answers) that would be appreciated as it makes posts much more useful for anyone else who comes across them. Thanks :slightly_smiling_face:.

Thanks for the feedback. Normally I would provide a gist or github repository link. But this was a general question regarding a new course exercise. I see your point
and will now provide the associated link.

1 Like

Hi Adam,

Thank you for the feedback. That was my conclusion as well. The codecademy single string value format will retain the integrity of a standard tuple, while my each value string format allows easier access to the values. I had looked to the file of the exercise but there was no test file or otherwise to examine for additional information. I assume as I progress with the course syntax, etc… will be expanded on.

Jeanine