I want to create a dictionary of even numbers with the keys being consecutive integers using dictionary comprehension
The output should be:
{1:2,2:4,3:6,4:8}
I used 2 lines of code ie one line being the list comprehension to get the even numbers and the second being the dictionary comprehension to get the desired output.
The code i used is as follows:
evens=[number for number in range(1,10) if number%2==0]
even_dict={k:evens[k-1] for k in range(1,len(evens)+1)}
My question is instead of using 2 lines of code, can we use a single line of code which involves only dictionary comprehension to get the desired output?
According to what is your desired output, you can simply do:
d = {x: 2*x for x in range(1, 5)}
The way you have it now, you have to define evens before since you are using it in two places in the dict comprehension: To iterate the indices, and to get the actual element. Generally, whenever you need both the index and the element, you can use enumerate instead, possible with start parameter if you want to offset the index:
even_dict = {i: x for i, x in enumerate(evens, start=1)}
Now you only need evens once, and thus you could "inline" it into the dict comprehension:
even_dict = {i: x for i, x in enumerate([number for number in range(1,10) if number%2==0], start=1)}
But you do not need that inner list comprehension at all; to get the even numbers, you could just use range with step parameter:
even_dict = {i: x for i, x in enumerate(range(2, 10, 2), start=1)}
And, finally, in this particular case, you would not even need that, either, as you can just multiply the key with two to get the value, as shown in #olinox's answer.
Related
So I have this dict i take from some page using request. Now i use its values to create list. How can I iterate on that list to extract and use each item?
I have already tried something like this:
for component in values:
if values.index(component) > 0:
value = values.pop()
but its give me only some items and leave others.
It looks like you only need to iterate over the list, not remove any elements.
If you want to create a list from an existing one, you can use the list comprehension:
same_values = [x for x in values]
And if you want, you can add a specific condition:
positive_values = [x for x in values if x > 0]
Given, you got a dictionary and need the values in the form of a list. Let dct be your dictionary, then extract the values from the dictionary...
list_values = list( dct.values() )
If you want to filter the values based on the index of the dictionary, then you can use the below code to filter the values based on the index from the dictionary.
for i, j in zip( dct.keys(), dct.values() ):
if i > 0:
print(j)
The functions keys() and values() returns the indexes and values at that index separately, which are joined using the zip function and stored into i and j respectively at each iteration of the for-loop.
I searched through stackoverflow but couldn't find an answer or adapt similar codes, unfortunately.
The problem is that I have a list of tuples:
tupl = [(5,3,33), (2,5,2), (4,1,7)]
and I should use list comprehension to have this output:
[8,7,5]
The code that I wrote is a bit dump and is:
sum_tupl = []
tupl = [(5,3,33), (2,5,2), (4,1,7)]
sum_tupl = [tupl[0][0]+tupl[0][1] for tuple in tupl]
sum_tupl
but instead of doing what I want it to do, it returns
[8,8,8]
which is the sum of the first couple executed three times.
I tried using a variant:
tupl = [(5,3,33), (2,5,2), (4,1,7)]
sum_tupl = [sum(tupl[0],tupl[1]) for tuple in tupl]
sum_tupl
(which is missing something) but to no avail.
Note: When you're beginning python, it's much easier to use loops instead of list comprehensions because then you can debug your code more easily, either by using print() statements or by stepping through it using a debugger.
Now, on to the answer:
When you do for x in y, with a list y, the individual items go in x. So for your code for tuple in tupl, you shouldn't use tupl because that is the entire list. You need to use the individual item in the list, i.e. tuple.
Note that it's not a good idea to name a variable tuple because that's already a builtin type in python.
You need:
tupl = [(5,3,33), (2,5,2), (4,1,7)]
sum_tupl = [t[0]+t[1] for t in tupl]
Which gives the list
sum_tupl: [8, 7, 5]
If you have more elements you want to sum, it makes more sense to use the sum() function and a slice of t instead of writing out everything. For example, if you wanted to sum the first 5 elements, you'd write
sum_tupl = [sum(t[0:5]) for t in tupl]
instead of
sum_tupl = [t[0]+t[1]+t[2]+t[3]+t[4] for t in tupl]
Each loop iteration you're accessing the first element of your tupl list, instead of using the current element. This is what you want:
sum_tupl = [t[0] + t[1] for t in tupl]
I prefer slicing:
>>> [x + y for x, y, *_ in tupl]
[8, 7, 5]
>>>
I'm using the following example for demonstration purposes.
[["apple",10],
["oranges",5],
["strawberies",2],
["pineapples",12],
["bananas",9],
["tomattoes",8],
["watermelon",1],
["mangos",7],
["grapes",11],
["potattoes",3]]
I want to get say the top 3 fruits by quantity (top 3 elements returned), however i don't want the order to change.
So the end result will be
[["apple",10],
["pineapples",12],
["grapes",11]]
Any help will be appreciated.
arr = [["apple",10],
["oranges",5],
["strawberies",2],
["pineapples",12],
["bananas",9],
["tomattoes",8],
["watermelon",1],
["mangos",7],
["grapes",11],
["potattoes",3]]
sorted_arr = sorted(arr,key=lambda x: x[1],reverse=True)[:3]
output = [elem for elem in arr if elem in sorted_arr]
print(sorted_arr)
print(output)
First, we sort the array in reverse order to get the first 3 elements. Then, we use list comprehension to loop through the original array and check if the elements are in the top 3. This preserves the order.
As you have probably heard before there is a sort method that you can apply to lists. You can pass in the key as a function that will indicate how you want your list to be sorted.
l = [["apple",10],
["oranges",5],
["strawberies",2],
["pineapples",12],
["bananas",9],
["tomattoes",8],
["watermelon",1],
["mangos",7],
["grapes",11],
["potattoes",3]]
l.sort(key = lambda x: x[1]) #to sort by second element
result = l[-3:] # To get the top three elements
I have a list of tuples with duplicates and I've converted them to a dictionary using this code I found here:
https://stackoverflow.com/a/61201134/2415706
mylist = [(a,1),(a,2),(b,3)]
result = {}
for i in mylist:
result.setdefault(i[0],[]).append(i[1])
print(result)
>>> result = {a:[1,2], b:[3]}
I recall learning that most for loops can be re-written as comprehensions so I wanted to practice but I've failed for the past hour to make one work.
I read this: https://stackoverflow.com/a/56011919/2415706 and now I haven't been able to find another library that does this but I'm also not sure if this comprehension I want to write is a bad idea since append mutates things.
Comprehension is meant to map items in a sequence independent of each other, and is not suitable for aggregations such as the case in your question, where the sub-list an item appends to depends on a sub-list that a previous item appends to.
You can produce the desired output with a nested comprehension if you must, but it would turn what would've been solved in O(n) time complexity with a loop into one that takes O(n ^ 2) instead:
{k: [v for s, v in mylist if s == k] for k, _ in mylist}
I am trying to create a function that when passed a list of names of any length, will return a list of only those names that are exactly four characters in length. I want the output list to be comma delimited.
I have attached code of what I have tried. I tried to get the function to iterate through the list parameter passed to the function 'x' and then if the length of that list value is four characters long, add it to the output list.
I expected the output list to be ['Ryan','Mary'] but it's just ['Ryan'] as is.
I'm new to all this and don't understand why what I've done isn't working.
output = []
def friend(x):
for i in range(len(x)):
if len(x[i]) == 4:
return output.append(x[i])
list = ["Ryan","Kieran","Jason","Mary"]
friend(list)
print(output)
You don't want to return so early. you are not giving your function enough passes through your list to get more than 1 element with 4 letters. You want to take time to build your list before returning it. I would build up the output list then, once we are done looping entirely, return it:
def friend(x):
output = []
for i in range(len(x)):
if len(x[i]) == 4:
output.append(x[i])
return output
list = ["Ryan","Kieran","Jason","Mary"]
myOutput = friend(list)
print(myOutput)```
There are actually numerous ways to tackle this issue
One way is list comprehension:
list = ["Ryan","Kieran","Jason","Mary"]
print([name for name in list if len(name) == 4]) # iterate list using variable name, check if len(name) == 4, and add it to the list if it's true
Another way is making use of yield:
def friend(x):
for i in range(len(x)):
if len(x[i]) == 4:
yield x[i] # note that this returns a generator, not a list. Basically, you can only iterate once
Another way, using list comprehension, you can shorten your function like :
def friend(x):
return [k for k in x if len(k)==4]
list_ = ["Ryan","Kieran","Jason","Mary"]
print(friend(list_))
OUTPUT :
['Ryan', 'Mary']
Firstly I want to add that you should not use list (and other types names) as variable name. Also alternatively to list comprehension you might use filter function, example usage:
lst = ["Ryan","Kieran","Jason","Mary"]
output = list(filter(lambda x:len(x)==4,lst))
print(output) #['Ryan', 'Mary']
Note that filter requires function which takes element of lst as argument and returns False (jettison) or True (retain). I want to note that personally I find list comprehension more readable in this case and I just wanted to show function filter which could be encountered in situation requiring choice of certain elements from list.
You can use a list comprehension
output = [name for name in list_of_names if len(name)==4]
print(output)