Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

List Comprehension Help- Python

Go to solution Solved by fizzlesticks,

You can use anything you would would use in a normal if statement.

if wkd[1] == 'Sunday' or wkd[1] == 'Saturday'

or

if wkd[1] in ('Sunday', 'Saturday')

I have a large list of sub lists, in the likes of this: [[date, name of day, high temperature, low temperature]]

 

In a list comprehension I need to grab the date and high temperature for only Saturday and Sunday

 

I can get the date for a specific day, however I'm having troubles retrieving the information for both Saturday and Sunday.

 

This is what I have so far:

weekends = [[wkd[0],wkd[2]] for wkd in weather_yxe_2018 if wkd[1] == "Sunday"]

As you can see, its just retrieving the values for Sunday so far. I'm not 100% sure what conditionals are appropriate for lists or how list comprehensions interpret them.

 

Any help is appreciated. I'm totally new to pythons list systems 

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: GTX 1080 || Memory: 16GB @ 3200 || Cooler: Noctua D15 || PSU: 750W EVGA G2 || Case: Define C

Link to post
Share on other sites

You can use anything you would would use in a normal if statement.

if wkd[1] == 'Sunday' or wkd[1] == 'Saturday'

or

if wkd[1] in ('Sunday', 'Saturday')

1474412270.2748842

Link to post
Share on other sites
2 minutes ago, fizzlesticks said:

You can use anything you would would use in a normal if statement.

if wkd[1] == 'Sunday' or wkd[1] == 'Saturday'

or

if wkd[1] in ('Sunday', 'Saturday')

Ooookay, that clears up a lot of stuff

 

Thanks!

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: GTX 1080 || Memory: 16GB @ 3200 || Cooler: Noctua D15 || PSU: 750W EVGA G2 || Case: Define C

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×