T. Python Fag

A better way to do all this if statements ??
Pic related.

Attached: fag.jpg (165x478, 16K)

t. someone who never did homework.

install gentoo

I did it.
But i can't find a better code with the same readability.

dont know if bait or not...
but if not:

for(int i = 5; i < 10; ++i)
out[i] = out[i] == None ? 0 : out[i];

assuming you got access to a c style inline if and proper for loops. if you dont, use c or c++.

it's not a bait, i have really this code in my app.
thank for your suggestion, personally i don't like to much c style code.
All i know is in python i can use:
for outs in out[5:10]:
if outs == None:
out = 0;
but can't work because override list out with 0.
I don't know how to override only the proper element in the list.

for i, c in enumerate(out):
if(not c):
out[i] = 0

thank user this is what i was loooking for!

Here's something clean

Attached: 2019-02-07-1549536199_screenshot_1366x768.jpg (1366x768, 59K)

the absolute state

way more readable!
Thanks user!

That spacing is absurd, especially on 1366x768.

>talks about readability before knowing absolute basics
Get a clue OP

yes im going to kms. Bye

Hey I didn't say that

I know user, but im trying to learn python, i know my code is shit, and i know that in this board everyone is an hacker. I just want to develop an idea i have since long time, and i want to write not the best code possible, just a code that works and a maintenable code in my spare time. I wuold like to learn every single trick in python but i can't, i have to do by myself tons of works to make my fucking app.
Im happy in this board i can find some humble user, who like help another user.
That's all.

>list([

i use arch btw

good, do it

0 if out [i] is None else out[i]
But I think the better way is just to use zeros when you declare the array. Also you are passing a list to list

[x if x else 0 for x in out]

Solved.
Thanks /g for the help

Attached: fag_2.jpg (417x127, 8K)

You're list cast is redundant. Your code is beyond retarded.

Fuck you OP

[0 if x is None else x for x in l]

Why don't you put 0 directly into your list?

You should test for None explicitly.

Empty lists and 0 vals will be matched in your code.

Ty user.

Holy fuck this image is the Jow Forums equivalent of someone posting a selife on Instagr*m

because i don't know a priori if it is none, and only element from 5 to 10 must be override with 0.
The code i posted is just to explain my problem.

I know. The example doesn't contain them tho

If replacement in only 5:10 you should slice the list in the list comprehension.

That's what happens when you use an improperly set up terminal emulator.

You should be very careful with code like this.
You are hardcoding a number of things, and making assumptions about the input.

You might be better off without the list comp

for i in range(5, len(out)):
if out[i] is None:
out[i] = 0

I know the comp is tempting, but explicit code will serve you better.

Also, please check out default arguments. You are passing this to a constructor or something right?

If not, use an object. Don't use untyped data.

absolute unit.py

>ask for readability
>confides in shortcuts
Absolute state of tards

thanks user, I'll test more my stuff to fully understand weakness in this code.

You are making assumptions about input by not checking out. Stop being a brainlet

le python software engineer

V e r y n i c e , a n o n ! E x e l e n t u s e o f l i s t c o m p r e h e n s i o n !

out = [ Name_1, Addr_1, Car_1, City_1, Airport_1, None, None, None, None, None ]
for o in out:
if o == None:
o = 0


or even more simple

out = [ Name_1, Addr_1, Car_1, City_1, Airport_1, 0, 0, 0, 0, 0 ]

>The example doesn't contain them tho
Hello, Ranjeet. How are you today?

I want to see a code example from people who check for invalid values in the input from their own values.

it's riced, did you expect something useful?

>i want to see a professional code example from a white man
no thanks, i charge for consulting.

Pass by heap, not reference. So o = 0 does nothing outside of the scope

You can't foreach an array, please kys.

The only consulting you need is a psychiatrist one which will help you to discern between code examples and real word applications with exceptions and type handling.

do your homework jamal

Use enumerate, holy fuck
Your language is built for slobbering regards and you still can't use it

In python exceptions are a part of life. You should NEVER check data types.

The existence of enumerate represents everything that is wrong with python.

The mouthbreating python devs try their best to contradict their own ideology and ruin what little value their toy language has.

Python in a nutshell desu.

It's become a sport for me to compact as many loops I can into comprehensions.

Maybe in your 10 lines script. Try building a project with at least 10000 lines, and you'll see that checking stuff out helps a lot

>he thinks meme language enthusiasts have any knowledge of real world code writing
ebin

The real problem here is that you're using a list of a harcoded length 10 to store your information. You have named fields, you shouldn't be using a list and hardcoding the fact that the defaults are 0 for fields 5 through 10, you should have a proper data structure with named fields.

you can also do 'if not val' instead of 'val is None'

That would match other falsy input. Maybe the empty string is a valid input.

> The real problem here is that you're using a list of a harcoded length 10 to store your information

why it's a problem the fixed lenght?

lmfao make it into class it does all that workd for ytou

class UserDetails:

def __init__(self, name=None, address=None, car=None):
self.name = name
self.address = address
self.car = car

# no need for stupid statements if its empty or not. Need to use keyword args to

newUser = UserDetails(name="Fag")

print(newUser.name, newUser.address)

# or even better, in 3.7+ use dataclasses

from dataclasses import dataclass, field

@dataclass
class UserDetails:

name: str = field(default=None)
address: str = field(default=None)

newUser = UserDetails(name="Fag")

as a bonus, if you wanna do that stupid iteration

from dataclasses import dataclass, field, astuple

@dataclass
class UserDetails:

name: str = field(default=None)
address: str = field(default=None)

# no need for stupid statements if its empty or not. Need to use keyword args to

newUser = UserDetails(name="Fag")

# bonus

a = list(astuple(newUser))

print(a)

# if you wanna do this, but its stupid
for element in a:
if element == None:
print("Dicks")

What the fuck is the deal with your character spacing?

Attached: 1433315010776.png (500x375, 149K)

Or you can just do this,

class UserDetails:

def __init__(self, name=None, address=None, car=None):
self.name = 0 if name is None else name
self.address = 0 if address is None else address
self.car = 0 if car is None else car


myUser = UserDetails("Harry", "Whitehouse")

print(myUser.name, myUser.car)

Y'all are dumb.
out = map(out, lambda x: 0 if not x else x)

Working with index numbers instead of names are stupid and makes for non-robust code

OOP

Lambda

Pick ur poison

There is so much wrong with OP's code

deadass thought about it after i clicked submit. dismiss it

This guy knows.
Dataclasses are my default go-to for fast n easy classes.

Using filter, map, or reduce when a list comprehension can do is unpythonic and discouraged.

kek the code i posted is just the example of my issue in this shit if statements.
It's not my real code, just an example to find a better way to handle this issue!
and i find it thanks to /g:
out[5:10] = [0 if val is None else val for val in out[5:10]]

every monkey who read this will understand what this does! and this is exactly what i want, and it worked well!

OOP and lambda is out of the scope of my request in this board. I have use oop and lambda in my project, but not this time to just clean a fucking array list.

You don't need to specify [5:10] at all... That's what the if/else is for.

print(str(out).replace("None", "0"))

no because if elem[1] for example is None, i want that remain None, only elements from 5 to 10 must be 0 if they are None. Im in a loop and for each line read i need to store and clean some information, i put it in out list and append each of them.
Then return the array list.

So why do you have all the extra None's at the end? Aren't you starting with an empty list and calling .append() to add things to it?

because i read a json, and save only some informations. Yes i can check if the value read is none before making list, but i prefer to store information exactly as read and after override the none from index 5 to 10.
And yes i start with an empty array arr = [],
then in a for loop out = list([a,b,c,d,e,f,g,h,i...])
override from index 5 to 10 all elements == none with 0 and arr.append(out).
at the end of the for return(arr)

`out = [a == None ? 0 : a for a in out]`

Or just use a dict? Classes in pythons are dicts anyway. You may as well use them to implement abstract data types. As long as you don't use inheritance you're not really using OOP.

You're missing the points. Lists are meant for easy appending/prepending/insertion. You shouldn't use a list to store a field with name, address, car, city, airport. You should use a dict or a data class. If this was C you would use a struct.

I used to do this, but objects are nicer for collection processing. Cleaner syntax due to less syntactic sugar. Data classes are your friend.

that would be the python-way of doing things

Definitely, but if you want to avoid objects for some reason then dicts work fine. Using methods like .get with defaults helps make it look reasonably nice.

>avoiding objects in a language where literally everything is an object
Sounds like some homework restriction BS and I can know, I taught Python as a TA

slower than list comprehension in python
don't pretend you're using haskell when you're not because you're too retarded

>then in a for loop
jesus christ just stop
why use python when you know nothing about it

David, get the fuck off fourchannel

this is the real code.
I know it could be better, but imo this code help me in the next years to remeber what this code do, and eventually this allow me to change fast if i choose another json provider.

Attached: fag_3.jpg (816x549, 62K)

That spacing hurts, you know.

Attached: image0.jpg (360x360, 30K)

>for eles in ele
>if eles=='status' and ele['status']=='FINISHED'
Retard. You're an absolute retard.

ty user it work, that's enough

loop from 5 to 9, and check in the loop

>this exists and runs on a real computer somewhere in the world

My god

kek yes on vm with a windows 7 os and a I5u laptop cpu.
I will call you for the audit.

>if eles=='status' and ele['status']=='FINISHED'

Where do you live OP so I can punch you right in the face

ur mother know where i live.

Why the fuck aren't you using a dictionary instead of a list? Whenever you're autistically pre-selecting slices in a list you really should just straight up go to something like a dict because it improved readability.

>year = 2018
This code exists somewhere in the world

Heads up, you're on the right path. 49.5% hating don't mean it and are just shitposting, 49.5% are bitter incels who are jealous that you still got the drive to do and learn something new and 1% REALLLY hates you and wants to see you suffer until you die.
If you keep trying and improving, maybe you can become one of the "hackers" in this board, hating on other people for unimportant shit.

i know user, that's why i like Jow Forums.
And i don't mind to be mocked, it's funny.

> for eles in ele
if eles=='status' and ele['status']=='FINISHED'

ele['status']=='FINISHED'

i mean

if ele['status']=='FINISHED'

kek you got me.
I will improve this shit.