Skip to content

Tag: list

Fastest way to check if a value exists in a list

What is the fastest way to check if a value exists in a very large list? Answer Clearest and fastest way to do it. You can also consider using a set, but constructing that set from your list may take more time than faster membership testing will save. The only way to be certain is to benchmark well. (this als…

Print a list in reverse order with range()?

How can you produce the following list with range() in Python? Answer use reversed() function: It’s much more meaningful. Update: If you want it to be a list (as btk pointed out): Update: If you want to use only range to achieve the same result, you can use all its parameters. range(start, stop, step) F…

Joining pairs of elements of a list [duplicate]

This question already has answers here: How to iterate over a list in chunks (39 answers) Closed 8 months ago. I know that a list can be joined to make one long string as in: Obviously this would output: However, what I am trying to do is simply join the first and second strings in the list, then join the

List sorting/modify problem

Initially, I wasn’t sure whether this was the appropriate place for this question, but after reading through the FAQ I feel fairly confident the topic is acceptable…. Furthermore I wasn’t sure if this would be classified as a particular type of problem (eg. a knapsack problem), thus, the tit…