Creating a dictionary from a csv file?

I believe the syntax you were looking for is as follows: import csv with open(‘coors.csv’, mode=”r”) as infile: reader = csv.reader(infile) with open(‘coors_new.csv’, mode=”w”) as outfile: writer = csv.writer(outfile) mydict = {rows[0]:rows[1] for rows in reader} Alternately, for python <= 2.7.1, you want: mydict = dict((rows[0],rows[1]) for rows in reader)

Are list-comprehensions and functional functions faster than “for loops”?

The following are rough guidelines and educated guesses based on experience. You should timeit or profile your concrete use case to get hard numbers, and those numbers may occasionally disagree with the below. A list comprehension is usually a tiny bit faster than the precisely equivalent for loop (that actually builds a list), most likely … Read more

Accessing class variables from a list comprehension in the class definition

Class scope and list, set or dictionary comprehensions, as well as generator expressions do not mix. The why; or, the official word on this In Python 3, list comprehensions were given a proper scope (local namespace) of their own, to prevent their local variables bleeding over into the surrounding scope (see List comprehension rebinds names … Read more

How can I use a conditional expression (expression with if and else) in a list comprehension? [duplicate]

x if y else z is the syntax for the expression you’re returning for each element. Thus you need: [ x if x%2 else x*100 for x in range(1, 10) ] The confusion arises from the fact you’re using a filter in the first example, but not in the second. In the second example you’re … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)