What’s the difference between lists enclosed by square brackets and parentheses in Python?

Square brackets are lists while parentheses are tuples. A list is mutable, meaning you can change its contents: >>> x = [1,2] >>> x.append(3) >>> x [1, 2, 3] while tuples are not: >>> x = (1,2) >>> x (1, 2) >>> x.append(3) Traceback (most recent call last): File “<stdin>”, line 1, in <module> AttributeError: … Read more

How to convert comma-delimited string to list in Python?

You can use the str.split method. >>> my_string = ‘A,B,C,D,E’ >>> my_list = my_string.split(“,”) >>> print my_list [‘A’, ‘B’, ‘C’, ‘D’, ‘E’] If you want to convert it to a tuple, just >>> print tuple(my_list) (‘A’, ‘B’, ‘C’, ‘D’, ‘E’) If you are looking to append to a list, try this: >>> my_list.append(‘F’) >>> print … Read more

Unpacking a list / tuple of pairs into two lists / tuples

>>> source_list = [(‘1′,’a’),(‘2′,’b’),(‘3′,’c’),(‘4′,’d’)] >>> list1, list2 = zip(*source_list) >>> list1 (‘1’, ‘2’, ‘3’, ‘4’) >>> list2 (‘a’, ‘b’, ‘c’, ‘d’) Edit: Note that zip(*iterable) is its own inverse: >>> list(source_list) == zip(*zip(*source_list)) True When unpacking into two lists, this becomes: >>> list1, list2 = zip(*source_list) >>> list(source_list) == zip(list1, list2) True Addition suggested by … Read more

Convert tuple to list and back

Convert tuple to list: >>> t = (‘my’, ‘name’, ‘is’, ‘mr’, ‘tuple’) >>> t (‘my’, ‘name’, ‘is’, ‘mr’, ‘tuple’) >>> list(t) [‘my’, ‘name’, ‘is’, ‘mr’, ‘tuple’] Convert list to tuple: >>> l = [‘my’, ‘name’, ‘is’, ‘mr’, ‘list’] >>> l [‘my’, ‘name’, ‘is’, ‘mr’, ‘list’] >>> tuple(l) (‘my’, ‘name’, ‘is’, ‘mr’, ‘list’)

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