My function returns a list with a single integer in it, how can I make it return only the integer?
Asked Answered
H

6

6

How do I remove the brackets from the result while keeping the function a single line of code?

day_list = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]

def day_to_number(inp):
    return [day for day in range(len(day_list)) if day_list[day] == inp]

print day_to_number("Sunday")
print day_to_number("Monday")
print day_to_number("Tuesday")
print day_to_number("Wednesday")
print day_to_number("Thursday")
print day_to_number("Friday")
print day_to_number("Saturday")

Output:

[0]
[1]
[2]
[3]
[4]
[5]
[6]
Heterogenesis answered 22/2, 2016 at 15:2 Comment(2)
Why not using a dictionary with key, value? You don't really want a list here.Fiddlewood
day_to_number returns a list. return only the element you want instead. if you know there will only be 1 element just grab [0]Phillie
A
13

The list comprehension is overkill. If your list does not contain duplicates (as your sample data shows, just do)

>>> def day_to_number(inp):
...     return day_list.index(inp)
... 
>>> day_to_number("Sunday")
0

I would also advice to make the day_list an argument of the function, i.e.:

>>> def day_to_number(inp, days):
...     return days.index(inp)
... 
>>> day_to_number("Sunday", day_list)
0

Looking it up in the global name space is a bit ugly.

And to make the whole thing more efficient (list.index is O(n)) use a dictionary:

>>> days = dict(zip(day_list, range(len(day_list))))
>>> days
{'Monday': 1, 'Tuesday': 2, 'Friday': 5, 'Wednesday': 3, 'Thursday': 4, 'Sunday': 0, 'Saturday': 6}
>>>
>>> def day_to_number(inp, days):
...     return days[inp]
... 
>>> day_to_number("Sunday", days)
0
Architecture answered 22/2, 2016 at 15:5 Comment(0)
A
6

Return the first item, not the list:

return [day for day in range(len(day_list)) if day_list[day] == inp][0]

But what you really want to do, is change your logic:

return day_list.index(inp)
Ampliate answered 22/2, 2016 at 15:3 Comment(1)
You beat me to it :(Roadrunner
R
3

Try this:

return [day for day in range(len(day_list)) if day_list[day] == inp][0]

However, this is not the most efficient way to achieve this. Try this instead:

day_list.index(inp)

I know it looks like I copied the other answer, I swear I didn't :)

Roadrunner answered 22/2, 2016 at 15:4 Comment(0)
T
3

This is a good use case for a dictionary:

>>> day_map = {day: index for index, day in enumerate(day_list)}

Use:

>>> day_map['Sunday']
0
>>> day_map['Tuesday']
2
Thunderstorm answered 22/2, 2016 at 15:10 Comment(0)
R
2

You could also use the calendar module, which has constants defined for the weekdays (in English):

>>> import calendar
>>> def day_to_number(day):
...     return getattr(calendar, day.upper())
... 
>>> day_to_number('wednesday') 
2 # Note that 0 = 'Monday'

If you are using this, you should probably add some error handling:

>>> day_to_number('epoch')
1970

Or:

>>> day_to_number('foo')
Traceback (most recent call last):
  File "<ipython-input-22-4649e99206ae>", line 1, in <module>
    day_to_number('foo')
  File "<ipython-input-14-bf0518eb14b5>", line 2, in day_to_number
    return getattr(calendar, day.upper())
AttributeError: 'module' object has no attribute 'FOO'
Resurrection answered 22/2, 2016 at 15:14 Comment(1)
FWIW, the datetime and calendar modules are locale-sensitive. So if you know how to use locales, you can get the day / month names for any locales that you have installed.Epanodos
H
1

This looks overly complicated. You don't need a list comprehension for this task. You can use the index() method:

>>> def day_to_number(inp):
...     return day_list.index(inp)
...
>>> day_to_number("Sunday")
0
>>> day_to_number("Monday")
1
>>> day_to_number("Saturday")
6

If you pass an invalid value, it will raise a ValueError

Herc answered 22/2, 2016 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.