This question already has answers here:
How can I get list of values from dict?
(7 answers)
Why does map return a map object instead of a list in Python 3?
(4 answers)
Closed 3 years ago.
I have recently started using python 3 and while working on list comprehension I am seeing something completely different from python 2. I want to know why is it that while printingthe value of 'y' python3 is returning [dict_values([1]), dict_values([2])] instead of [1,2] and how I can get response in desired form i.e,[1,2]
>>> x=[{'x':1},{'y':2}]
>>> y=[i.values() for i in x]
>>> y
[dict_values([1]), dict_values([2])]
Related
This question already has answers here:
What does it mean to unpack in python?
(5 answers)
List comprehension rebinds names even after scope of comprehension. Is this right?
(6 answers)
Closed 5 months ago.
I was trying to make a code that allows multiple user inputs in different variables in only one line in Python. I've studied a bit of list comprehension, but I can't understand how the code below works.
a,b,c = [int(a) for a in input().split()]
Why is the output an integer value of a, b, and c if only a is being put into the loop?
This question already has answers here:
Slice every string in list in Python
(3 answers)
What is the purpose of the two colons in this Python string-slicing statement?
(1 answer)
Closed 4 years ago.
If i had a list as an example:
a = ['Hello_1.txt', 'Hello_2.txt']
In Python is it possible to somehow remove the first 5 characters ('Hello')
and the last 3 characters ('txt') from each of the items in the list ?
You could use a list-comprehension and string slicing:
[s[5:-3] for s in a]
which gives what you describe (not sure this is the neatest output though!)
['_1.', '_2.']
This question already has answers here:
If in Python I put a list inside a tuple, can I safely change the contents of that list? [duplicate]
(2 answers)
Why can tuples contain mutable items?
(8 answers)
a mutable type inside an immutable container
(3 answers)
Closed 4 years ago.
I am new in Python and I need some clarity on this.
I read everywhere that tuples are immutable, so they cannot be changed, however I did this test, and I could change the values inside a list within a tuple, am I doing something wrong? Or why am I able to change this value? Thanks.
This is my test code:
>tuplelist = ('mouse',[1,2,3])
**('mouse', [1, 2, 3])**
>tuplelist[1][0]=999;
**('mouse', [999, 2, 3])**
This question already has answers here:
How can I make a dictionary (dict) from separate lists of keys and values?
(21 answers)
Closed 7 years ago.
I have two list of same length l1=[reboot,revision,disk_space] and l2=[no,12.15,4300] I want to make these two list as key-value map. Output should be like this [reboot:no,revision:12.18,disk_space:4300].. I tried Zip it is not working
You can create a dictionary from the output of zip (Thanks, Martijn!):
print dict(zip(l1, l2))
This question already has answers here:
Python list problem [duplicate]
(2 answers)
Nested List Indices [duplicate]
(2 answers)
Closed 9 years ago.
I've noticed something strange when trying to implement elements of a 2d list in python. Here is my code
n_dk=2*[5*[0]]
n_dk[0][0]+=1
print n_dk
I would expect the output to be
[[1,0,0,0,0],[0,0,0,0,0]]
but the actual output is
[[1,0,0,0,0],[1,0,0,0,0]]
can anyone tell me what I am doing wrong. btw I used a numpy array instead and it worked the way I wanted it to.