## Iterating through list of list

This traverse generator function can be used to iterate over all the values: def traverse(o, tree_types=(list, tuple)): if isinstance(o, tree_types): for value in o: for subvalue in traverse(value, tree_types): yield subvalue else: yield o data = [(1,1,(1,1,(1,”1″))),(1,1,1),(1,),1,(1,(1,(“1”,)))] print list(traverse(data)) # prints [1, 1, 1, 1, 1, ‘1’, 1, 1, 1, 1, 1, 1, 1, ‘1’] … Read more

## Check list monotonicity

Are repeated values (e.g. [1, 1, 2]) monotonic? If yes: def non_decreasing(L): return all(x<=y for x, y in zip(L, L[1:])) def non_increasing(L): return all(x>=y for x, y in zip(L, L[1:])) def monotonic(L): return non_decreasing(L) or non_increasing(L) If no: def strictly_increasing(L): return all(x<y for x, y in zip(L, L[1:])) def strictly_decreasing(L): return all(x>y for x, y … Read more

## How do I convert all of the items in a list to floats? [duplicate]

[float(i) for i in lst] to be precise, it creates a new list with float values. Unlike the map approach it will work in py3k.

## Find a value in a list [duplicate]

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

## Iterate through adjacent pairs of items in a Python list [duplicate]

You can zip the list with itself sans the first element: a = [5, 7, 11, 4, 5] for previous, current in zip(a, a[1:]): print(previous, current) This works even if your list has no elements or only 1 element (in which case zip returns an empty iterable and the code in the for loop never … Read more

## Extract first item of each sublist in Python

Using list comprehension: >>> lst = [[‘a’,’b’,’c’], [1,2,3], [‘x’,’y’,’z’]] >>> lst2 = [item[0] for item in lst] >>> lst2 [‘a’, 1, ‘x’]

## How to sort a list of dictionaries by a value of the dictionary in Python?

The sorted() function takes a key= parameter newlist = sorted(list_to_be_sorted, key=lambda d: d[‘name’]) Alternatively, you can use operator.itemgetter instead of defining the function yourself from operator import itemgetter newlist = sorted(list_to_be_sorted, key=itemgetter(‘name’)) For completeness, add reverse=True to sort in descending order newlist = sorted(list_to_be_sorted, key=itemgetter(‘name’), reverse=True)

## Strange result when removing item from a list while iterating over it in Python

You’re modifying the list while you iterate over it. That means that the first time through the loop, i == 1, so 1 is removed from the list. Then the for loop goes to the second item in the list, which is not 2, but 3! Then that’s removed from the list, and then the … Read more

## How to remove List Separator lines in SwiftUI 2.0 in iOS 14 and above

Here is a demo of possible solution. Tested with Xcode 12b. List { ForEach(0..<3) { _ in VStack { Text(“Hello, World!”).padding(.leading) } .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading) .listRowInsets(EdgeInsets()) .background(Color(UIColor.systemBackground))) } }

## sum each value in a list of tuples

Use zip() and sum(): In [1]: l = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 0)] In [2]: [sum(x) for x in zip(*l)] Out[2]: [25, 20] or: In [4]: map(sum, zip(*l)) Out[4]: [25, 20] timeit results: In [16]: l = [(1, 2), (3, 4), (5, 6), (7, 8), (9, 0)]*1000 In [17]: %timeit … Read more