C# List to string with delimiter

You can use String.Join. If you have a List<string> then you can call ToArray first: List<string> names = new List<string>() { “John”, “Anna”, “Monica” }; var result = String.Join(“, “, names.ToArray()); In .NET 4 you don’t need the ToArray anymore, since there is an overload of String.Join that takes an IEnumerable<string>. Results: John, Anna, Monica

IEnumerable vs List – What to Use? How do they work?

IEnumerable describes behavior, while List is an implementation of that behavior. When you use IEnumerable, you give the compiler a chance to defer work until later, possibly optimizing along the way. If you use ToList() you force the compiler to reify the results right away. Whenever I’m “stacking” LINQ expressions, I use IEnumerable, because by … Read more

Python: Find in list

As for your first question: “if item is in my_list:” is perfectly fine and should work if item equals one of the elements inside my_list. The item must exactly match an item in the list. For instance, “abc” and “ABC” do not match. Floating point values in particular may suffer from inaccuracy. For instance, 1 … Read more

How to sort a list/tuple of lists/tuples by the element at a given index?

sorted_by_second = sorted(data, key=lambda tup: tup[1]) or: data.sort(key=lambda tup: tup[1]) # sorts in place The default sort mode is ascending. To sort in descending order use the option reverse=True: sorted_by_second = sorted(data, key=lambda tup: tup[1], reverse=True) or: data.sort(key=lambda tup: tup[1], reverse=True) # sorts in place

Shuffling a list of objects

random.shuffle should work. Here’s an example, where the objects are lists: from random import shuffle x = [[i] for i in range(10)] shuffle(x) print(x) # print(x) gives [[9], [2], [7], [0], [4], [5], [3], [1], [8], [6]] Note that shuffle works in place, and returns None. More generally in Python, mutable objects can be passed … Read more

Writing a list to a file with Python, with newlines

Use a loop: with open(‘your_file.txt’, ‘w’) as f: for line in lines: f.write(f”{line}\n”) For Python <3.6: with open(‘your_file.txt’, ‘w’) as f: for line in lines: f.write(“%s\n” % line) For Python 2, one may also use: with open(‘your_file.txt’, ‘w’) as f: for line in lines: print >> f, line If you’re keen on a single function … Read more

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