Skip to content
Advertisement

Tag: list

Calculating arithmetic mean (one type of average) in Python [duplicate]

This question already has answers here: Finding the average of a list (25 answers) Closed 12 months ago. Is there a built-in or standard library method in Python to calculate the arithmetic mean (one type of average) of a list of numbers? Answer I am not aware of anything in the standard library. However, you could use something like: In

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 also

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) For example, to generate a list

How to Query model where name contains any word in python list?

Aim to Achieve: I want all objects where name attribute contains any word from the list. I have: For example: if name=”this is word2″: Then object with such a name should be returned since word2 is in the list. Please help! Answer You could use Q objects to constuct a query like this: Edit: is a fancy way to write

Dictionary keys match on list; get key/value pair

In python… I have a list of elements ‘my_list’, and a dictionary ‘my_dict’ where some keys match in ‘my_list’. I would like to search the dictionary and retrieve key/value pairs for the keys matching the ‘my_list’ elements. I tried this… But it doesn’t do the job. Answer (I renamed list to my_list and dict to my_dict to avoid the conflict

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

[] and {} vs list() and dict(), which is better? [closed]

Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 6 months ago. The community reviewed whether to reopen this question 6 months ago and left it closed: Original close reason(s) were not resolved Improve this question

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 title is rather vague. I’m sorry for that. Anyways. As both practice

Advertisement