Skip to main content
Commonmark migration
Source Link

This answer is less about the code you have produced, but more about how you could approach the problem if you were to do it again.

note, the python style guides promote snake_case over camelCase naming conventions, so I'll use these here.

"Say you have a list value like this: spam = ['apples', 'bananas', 'tofu', 'cats']

 

Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with and inserted before the last item.

What is the key functionality that needs to be done here?

Simply put, the main functionality of the code is to take the values in the list and convert this into a string.
This should immediately suggest that string.join() would be a good function for this.

As it turns out, we can join the strings with whatever sub-string we like, so by using

', '.join(list_of_strings)

we get the following transformation:

['apples', 'bananas', 'tofu', 'cats'] -> 'apples, bananas, tofu, cats'

That's almost the entire task complete! ('join()' handles the 0 and 1 length list arrays correctly, so you don't have to)
We just need to insert the 'and '.

A quick analysis of the problem shows that we only need the 'and ' when there are at least two items, so we write a modification to do just that.
We could just add 'and' in the penultimate location in the list, but we don't want to end up with ['x', 'y'] -> 'x, and, y' so the simple solution to this is to replace the final input in this case with 'and ' plus the input.

We can do this with one of the following lines:

#python 2+:
list_of_strings[-1] = 'and %s' % list_of_strings[-1]
#python 2.6+:
list_of_strings[-1] = 'and {}'.format(list_of_strings[-1])
#python 3.6+:
list_of_strings[-1] = f'and {list_of_strings[-1]}'

To ensure that this does not change the input (it may be reused elsewhere) we should make a copy of it first. The simple way is to create a new list from the original:

list_of_strings = list(input_list_of_strings)

Putting this all together, we get a fairly simple function as a result:

def comma_code(input_list_of_strings):
    list_of_strings = list(input_list_of_strings)
    if len(list_of_strings) > 1:
        list_of_strings[-1] = f'and {list_of_strings[-1]}'
    return ', '.join(list_of_strings)

This answer is less about the code you have produced, but more about how you could approach the problem if you were to do it again.

note, the python style guides promote snake_case over camelCase naming conventions, so I'll use these here.

"Say you have a list value like this: spam = ['apples', 'bananas', 'tofu', 'cats']

 

Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with and inserted before the last item.

What is the key functionality that needs to be done here?

Simply put, the main functionality of the code is to take the values in the list and convert this into a string.
This should immediately suggest that string.join() would be a good function for this.

As it turns out, we can join the strings with whatever sub-string we like, so by using

', '.join(list_of_strings)

we get the following transformation:

['apples', 'bananas', 'tofu', 'cats'] -> 'apples, bananas, tofu, cats'

That's almost the entire task complete! ('join()' handles the 0 and 1 length list arrays correctly, so you don't have to)
We just need to insert the 'and '.

A quick analysis of the problem shows that we only need the 'and ' when there are at least two items, so we write a modification to do just that.
We could just add 'and' in the penultimate location in the list, but we don't want to end up with ['x', 'y'] -> 'x, and, y' so the simple solution to this is to replace the final input in this case with 'and ' plus the input.

We can do this with one of the following lines:

#python 2+:
list_of_strings[-1] = 'and %s' % list_of_strings[-1]
#python 2.6+:
list_of_strings[-1] = 'and {}'.format(list_of_strings[-1])
#python 3.6+:
list_of_strings[-1] = f'and {list_of_strings[-1]}'

To ensure that this does not change the input (it may be reused elsewhere) we should make a copy of it first. The simple way is to create a new list from the original:

list_of_strings = list(input_list_of_strings)

Putting this all together, we get a fairly simple function as a result:

def comma_code(input_list_of_strings):
    list_of_strings = list(input_list_of_strings)
    if len(list_of_strings) > 1:
        list_of_strings[-1] = f'and {list_of_strings[-1]}'
    return ', '.join(list_of_strings)

This answer is less about the code you have produced, but more about how you could approach the problem if you were to do it again.

note, the python style guides promote snake_case over camelCase naming conventions, so I'll use these here.

"Say you have a list value like this: spam = ['apples', 'bananas', 'tofu', 'cats']

Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with and inserted before the last item.

What is the key functionality that needs to be done here?

Simply put, the main functionality of the code is to take the values in the list and convert this into a string.
This should immediately suggest that string.join() would be a good function for this.

As it turns out, we can join the strings with whatever sub-string we like, so by using

', '.join(list_of_strings)

we get the following transformation:

['apples', 'bananas', 'tofu', 'cats'] -> 'apples, bananas, tofu, cats'

That's almost the entire task complete! ('join()' handles the 0 and 1 length list arrays correctly, so you don't have to)
We just need to insert the 'and '.

A quick analysis of the problem shows that we only need the 'and ' when there are at least two items, so we write a modification to do just that.
We could just add 'and' in the penultimate location in the list, but we don't want to end up with ['x', 'y'] -> 'x, and, y' so the simple solution to this is to replace the final input in this case with 'and ' plus the input.

We can do this with one of the following lines:

#python 2+:
list_of_strings[-1] = 'and %s' % list_of_strings[-1]
#python 2.6+:
list_of_strings[-1] = 'and {}'.format(list_of_strings[-1])
#python 3.6+:
list_of_strings[-1] = f'and {list_of_strings[-1]}'

To ensure that this does not change the input (it may be reused elsewhere) we should make a copy of it first. The simple way is to create a new list from the original:

list_of_strings = list(input_list_of_strings)

Putting this all together, we get a fairly simple function as a result:

def comma_code(input_list_of_strings):
    list_of_strings = list(input_list_of_strings)
    if len(list_of_strings) > 1:
        list_of_strings[-1] = f'and {list_of_strings[-1]}'
    return ', '.join(list_of_strings)
added 2 characters in body
Source Link
Baldrickk
  • 324
  • 2
  • 10

This answer is less about the code you have produced, but more about how you could approach the problem if you were to do it again.

note, the python style guides promote snake_case over camelCase naming conventions, so I'll use these here.

"Say you have a list value like this: spam = ['apples', 'bananas', 'tofu', 'cats']

Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with and inserted before the last item.

What is the key functionality that needs to be done here?

Simply put, the main functionality of the code is to take the values in the list and convert this into a string.
This should immediately suggest that string.join() would be a good function for this.

As it turns out, we can join the strings with whatever sub-string we like, so by using

', '.join(list_of_strings)

we get the following transformation:

['apples', 'bananas', 'tofu', 'cats'] -> 'apples, bananas, tofu, cats'

That's almost the entire task complete! ('join()' handles the 0 and 1 length list arrays correctly, so you don't have to)
We just need to insert the 'and '.

A quick analysis of the problem shows that we only need the 'and ' when there are at least two items, so we write a modification to do just that.
We could just add 'and' in the penultimate location in the list, but we don't want to end up with ['x', 'y'] -> 'x, and, y' so the simple solution to this is to replace the final input in this case with 'and ' plus the input.

We can do this with one of the following lines:

#python 2+:
list_of_strings[-1] = 'and %s' % list_of_strings[-1]
#python 3+2.6+:
list_of_strings[-1] = 'and {}'.format(list_of_strings[-1])
#python 3.6+:
list_of_strings[-1] = f'and {list_of_strings[-1]}'

To ensure that this does not change the input (it may be reused elsewhere) we should make a copy of it first. The simple way is to create a new list from the original:

list_of_strings = list(input_list_of_strings)

Putting this all together, we get a fairly simple function as a result:

def comma_code(input_list_of_strings):
    list_of_strings = list(input_list_of_strings)
    if len(list_of_strings) > 1:
        list_of_strings[-1] = f'and {list_of_strings[-1]}'
    return ', '.join(list_of_strings)

This answer is less about the code you have produced, but more about how you could approach the problem if you were to do it again.

note, the python style guides promote snake_case over camelCase naming conventions, so I'll use these here.

"Say you have a list value like this: spam = ['apples', 'bananas', 'tofu', 'cats']

Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with and inserted before the last item.

What is the key functionality that needs to be done here?

Simply put, the main functionality of the code is to take the values in the list and convert this into a string.
This should immediately suggest that string.join() would be a good function for this.

As it turns out, we can join the strings with whatever sub-string we like, so by using

', '.join(list_of_strings)

we get the following transformation:

['apples', 'bananas', 'tofu', 'cats'] -> 'apples, bananas, tofu, cats'

That's almost the entire task complete! ('join()' handles the 0 and 1 length list arrays correctly, so you don't have to)
We just need to insert the 'and '.

A quick analysis of the problem shows that we only need the 'and ' when there are at least two items, so we write a modification to do just that.
We could just add 'and' in the penultimate location in the list, but we don't want to end up with ['x', 'y'] -> 'x, and, y' so the simple solution to this is to replace the final input in this case with 'and ' plus the input.

We can do this with one of the following lines:

#python 2+:
list_of_strings[-1] = 'and %s' % list_of_strings[-1]
#python 3+:
list_of_strings[-1] = 'and {}'.format(list_of_strings[-1])
#python 3.6+:
list_of_strings[-1] = f'and {list_of_strings[-1]}'

To ensure that this does not change the input (it may be reused elsewhere) we should make a copy of it first. The simple way is to create a new list from the original:

list_of_strings = list(input_list_of_strings)

Putting this all together, we get a fairly simple function as a result:

def comma_code(input_list_of_strings):
    list_of_strings = list(input_list_of_strings)
    if len(list_of_strings) > 1:
        list_of_strings[-1] = f'and {list_of_strings[-1]}'
    return ', '.join(list_of_strings)

This answer is less about the code you have produced, but more about how you could approach the problem if you were to do it again.

note, the python style guides promote snake_case over camelCase naming conventions, so I'll use these here.

"Say you have a list value like this: spam = ['apples', 'bananas', 'tofu', 'cats']

Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with and inserted before the last item.

What is the key functionality that needs to be done here?

Simply put, the main functionality of the code is to take the values in the list and convert this into a string.
This should immediately suggest that string.join() would be a good function for this.

As it turns out, we can join the strings with whatever sub-string we like, so by using

', '.join(list_of_strings)

we get the following transformation:

['apples', 'bananas', 'tofu', 'cats'] -> 'apples, bananas, tofu, cats'

That's almost the entire task complete! ('join()' handles the 0 and 1 length list arrays correctly, so you don't have to)
We just need to insert the 'and '.

A quick analysis of the problem shows that we only need the 'and ' when there are at least two items, so we write a modification to do just that.
We could just add 'and' in the penultimate location in the list, but we don't want to end up with ['x', 'y'] -> 'x, and, y' so the simple solution to this is to replace the final input in this case with 'and ' plus the input.

We can do this with one of the following lines:

#python 2+:
list_of_strings[-1] = 'and %s' % list_of_strings[-1]
#python 2.6+:
list_of_strings[-1] = 'and {}'.format(list_of_strings[-1])
#python 3.6+:
list_of_strings[-1] = f'and {list_of_strings[-1]}'

To ensure that this does not change the input (it may be reused elsewhere) we should make a copy of it first. The simple way is to create a new list from the original:

list_of_strings = list(input_list_of_strings)

Putting this all together, we get a fairly simple function as a result:

def comma_code(input_list_of_strings):
    list_of_strings = list(input_list_of_strings)
    if len(list_of_strings) > 1:
        list_of_strings[-1] = f'and {list_of_strings[-1]}'
    return ', '.join(list_of_strings)
creating a new list with `list(a_list)` is better than `a_list[:]`
Source Link
Baldrickk
  • 324
  • 2
  • 10

This answer is less about the code you have produced, but more about how you could approach the problem if you were to do it again.

note, the python style guides promote snake_case over camelCase naming conventions, so I'll use these here.

"Say you have a list value like this: spam = ['apples', 'bananas', 'tofu', 'cats']

Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with and inserted before the last item.

What is the key functionality that needs to be done here?

Simply put, the main functionality of the code is to take the values in the list and convert this into a string.
This should immediately suggest that string.join() would be a good function for this.

As it turns out, we can join the strings with whatever sub-string we like, so by using

', '.join(list_of_strings)

we get the following transformation:

['apples', 'bananas', 'tofu', 'cats'] -> 'apples, bananas, tofu, cats'

That's almost the entire task complete! ('join()' handles the 0 and 1 length list arrays correctly, so you don't have to)
We just need to insert the 'and '.

A quick analysis of the problem shows that we only need the 'and ' when there are at least two items, so we write a modification to do just that.
We could just add 'and' in the penultimate location in the list, but we don't want to end up with ['x', 'y'] -> 'x, and, y' so the simple solution to this is to replace the final input in this case with 'and ' plus the input.

We can do this with one of the following lines:

#python 2+:
list_of_strings[-1] = 'and %s' % list_of_strings[-1]
#python 3+:
list_of_strings[-1] = 'and {}'.format(list_of_strings[-1])
#python 3.6+:
list_of_strings[-1] = f'and {list_of_strings[-1]}'

To ensure that this does not change the input (it may be reused elsewhere) we should make a copy of it first. The simple way is with slicingto create a new list from the original:

list_of_strings = input_list_of_strings[:]list(input_list_of_strings)

Putting this all together, we get a fairly simple function as a result:

def comma_code(input_list_of_strings):
    list_of_strings = input_list_of_strings[:]list(input_list_of_strings)
    if len(list_of_strings) > 1:
        list_of_strings[-1] = f'and {list_of_strings[-1]}'
    return ', '.join(list_of_strings)

This answer is less about the code you have produced, but more about how you could approach the problem if you were to do it again.

note, the python style guides promote snake_case over camelCase naming conventions, so I'll use these here.

"Say you have a list value like this: spam = ['apples', 'bananas', 'tofu', 'cats']

Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with and inserted before the last item.

What is the key functionality that needs to be done here?

Simply put, the main functionality of the code is to take the values in the list and convert this into a string.
This should immediately suggest that string.join() would be a good function for this.

As it turns out, we can join the strings with whatever sub-string we like, so by using

', '.join(list_of_strings)

we get the following transformation:

['apples', 'bananas', 'tofu', 'cats'] -> 'apples, bananas, tofu, cats'

That's almost the entire task complete! ('join()' handles the 0 and 1 length list arrays correctly, so you don't have to)
We just need to insert the 'and '.

A quick analysis of the problem shows that we only need the 'and ' when there are at least two items, so we write a modification to do just that.
We could just add 'and' in the penultimate location in the list, but we don't want to end up with ['x', 'y'] -> 'x, and, y' so the simple solution to this is to replace the final input in this case with 'and ' plus the input.

We can do this with one of the following lines:

#python 2+:
list_of_strings[-1] = 'and %s' % list_of_strings[-1]
#python 3+:
list_of_strings[-1] = 'and {}'.format(list_of_strings[-1])
#python 3.6+:
list_of_strings[-1] = f'and {list_of_strings[-1]}'

To ensure that this does not change the input (it may be reused elsewhere) we should make a copy of it first. The simple way is with slicing:

list_of_strings = input_list_of_strings[:]

Putting this all together, we get a fairly simple function as a result:

def comma_code(input_list_of_strings):
    list_of_strings = input_list_of_strings[:]
    if len(list_of_strings) > 1:
        list_of_strings[-1] = f'and {list_of_strings[-1]}'
    return ', '.join(list_of_strings)

This answer is less about the code you have produced, but more about how you could approach the problem if you were to do it again.

note, the python style guides promote snake_case over camelCase naming conventions, so I'll use these here.

"Say you have a list value like this: spam = ['apples', 'bananas', 'tofu', 'cats']

Write a function that takes a list value as an argument and returns a string with all the items separated by a comma and a space, with and inserted before the last item.

What is the key functionality that needs to be done here?

Simply put, the main functionality of the code is to take the values in the list and convert this into a string.
This should immediately suggest that string.join() would be a good function for this.

As it turns out, we can join the strings with whatever sub-string we like, so by using

', '.join(list_of_strings)

we get the following transformation:

['apples', 'bananas', 'tofu', 'cats'] -> 'apples, bananas, tofu, cats'

That's almost the entire task complete! ('join()' handles the 0 and 1 length list arrays correctly, so you don't have to)
We just need to insert the 'and '.

A quick analysis of the problem shows that we only need the 'and ' when there are at least two items, so we write a modification to do just that.
We could just add 'and' in the penultimate location in the list, but we don't want to end up with ['x', 'y'] -> 'x, and, y' so the simple solution to this is to replace the final input in this case with 'and ' plus the input.

We can do this with one of the following lines:

#python 2+:
list_of_strings[-1] = 'and %s' % list_of_strings[-1]
#python 3+:
list_of_strings[-1] = 'and {}'.format(list_of_strings[-1])
#python 3.6+:
list_of_strings[-1] = f'and {list_of_strings[-1]}'

To ensure that this does not change the input (it may be reused elsewhere) we should make a copy of it first. The simple way is to create a new list from the original:

list_of_strings = list(input_list_of_strings)

Putting this all together, we get a fairly simple function as a result:

def comma_code(input_list_of_strings):
    list_of_strings = list(input_list_of_strings)
    if len(list_of_strings) > 1:
        list_of_strings[-1] = f'and {list_of_strings[-1]}'
    return ', '.join(list_of_strings)
added 82 characters in body
Source Link
Baldrickk
  • 324
  • 2
  • 10
Loading
Source Link
Baldrickk
  • 324
  • 2
  • 10
Loading