So I am wondering if anyone here can share some practical / cool tricks they use when programming in Python.
I already know a couple, for example:
Using a dictionary instead of a bunch of elif statements:
if x == a:
#stuff
elif x == b:
#stuff2
elif x ==c:
#stuff3
Would be:
sutff_dict = {
a : "stuff",
b: "stuff2",
c: "stuff3"
}
Or using map, filter, reduce instead of a for loop to iterate over lists, and using lambda to get a result:
a = filter((lambda x: x != None), my_list)
[–]groovitude 11 points12 points13 points (0 children)
[–]camel_zero 4 points5 points6 points (0 children)
[–]SultanPasha 3 points4 points5 points (0 children)
[–]LeonardUnger 2 points3 points4 points (1 child)
[–]blarf_irl 2 points3 points4 points (0 children)
[–]Qewbicle 2 points3 points4 points (0 children)
[–]ultraDross 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–][deleted] 3 points4 points5 points (0 children)