kdt 50
New Member
Can anyone suggest a better way of returning the values in a dictionary as a single list. I have the following, but it uses a nested loop, not sure if there is a more efficient way. -
>>> d['a']= [(45,6)]
-
>>> d['a'].append((56,4))
-
>>> d['a']
-
[(45, 6), (56, 4)]
-
>>> s =[]
-
>>> for i, j in enumerate(d['a']):
-
... for k, l in enumerate(j):
-
... s.append(l)
-
...
-
>>> s
-
[45, 6, 56, 4]
-
>>>
-
thanks
6 1834
Can anyone suggest a better way of returning the values in a dictionary as a single list. I have the following, but it uses a nested loop, not sure if there is a more efficient way. -
>>> d['a']= [(45,6)]
-
>>> d['a'].append((56,4))
-
>>> d['a']
-
[(45, 6), (56, 4)]
-
>>> s =[]
-
>>> for i, j in enumerate(d['a']):
-
... for k, l in enumerate(j):
-
... s.append(l)
-
...
-
>>> s
-
[45, 6, 56, 4]
-
>>>
-
thanks
-
s = [value for tup in d['a'] for value in tup]
-
Hope that helps.
bartonc 6,596
Recognized Expert Expert
Can anyone suggest a better way of returning the values in a dictionary as a single list. I have the following, but it uses a nested loop, not sure if there is a more efficient way. -
>>> d['a']= [(45,6)]
-
>>> d['a'].append((56,4))
-
>>> d['a']
-
[(45, 6), (56, 4)]
-
>>> s =[]
-
>>> for i, j in enumerate(d['a']):
-
... for k, l in enumerate(j):
-
... s.append(l)
-
...
-
>>> s
-
[45, 6, 56, 4]
-
>>>
-
thanks
-
>>> aList = [(45, 6), (56, 4)]
-
>>> newList = []
-
>>> for tup in aList:
-
... newList.extend(tup)
-
...
-
>>> newList
-
[45, 6, 56, 4]
-
>>>
kdt 50
New Member
Sweet, thanks. Much better!
bartonc 6,596
Recognized Expert Expert
Sweet, thanks. Much better!
You did see mine, right? One minute apart, maybe you were posting while I was.
-
>>> aList = [(45, 6), (56, 4)]
-
>>> newList = []
-
>>> for tup in aList:
-
... newList.extend(tup)
-
...
-
>>> newList
-
[45, 6, 56, 4]
-
>>>
Just for fun, you could do that with map: -
>>> aList = [(45, 6), (56, 4)]
-
>>> newList = []
-
>>> map(newList.extend, aList)
-
>>> newList
-
[45, 6, 56, 4]
-
bartonc 6,596
Recognized Expert Expert
Just for fun, you could do that with map: -
>>> aList = [(45, 6), (56, 4)]
-
>>> newList = []
-
>>> map(newList.extend, aList)
-
>>> newList
-
[45, 6, 56, 4]
-
Bonus points to you, my friend! Two lines in one; very nicely done.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Martin DeMello |
last post by:
It seems to be a fairly common pattern for an object-modifying method to
return None - however, this is often quite inconvenient.
For instance
def f(lst1, lst2):
g((lst1 + lst2).reverse()) # doesn't work!
you need to say
|
by: Raymond Hettinger |
last post by:
I would like to get everyone's thoughts on two new dictionary methods:
def count(self, value, qty=1):
try:
self += qty
except KeyError:
self = qty
def appendlist(self, key, *values):
try:
|
by: Christoph Zwerschke |
last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist
only since Python 2.4.
Anyway, I was thinking about whether it would be possible and desirable
to change the old behavior in future Python versions and let dict.keys()
and dict.values() both return sets instead of lists.
If d is a dict, code like:
for x in d.keys():
|
by: vatamane |
last post by:
This has been bothering me for a while. Just want to find out if it
just me or perhaps others have thought of this too: Why shouldn't the
keyset of a dictionary be represented as a set instead of a list? I
know that sets were introduced a lot later and lists/dictionaries were
used instead but I think "the only correct way" now is for the...
|
by: O.B. |
last post by:
I need the ability to parse through the values of a Dictionary and
remove certain ones depending on their attribute values. In the example
below, an InvalidOperationException is thrown in the foreach statement
when the first item is removed from the Dictionary.
From looking at Dictionary's methods, I couldn't find anything to
create a copy...
| |
by: Andrew Robinson |
last post by:
I have a method that needs to return either a Dictionary<k,vor a List<v>
depending on input parameters and options to the method.
1. Is there any way to convert from a dictionary to a list without
itterating through the entire collection and building up a list?
2. is there a common base class, collection or interface that can contain...
|
by: =?utf-8?B?TWFjaWVqIEJsaXppxYRza2k=?= |
last post by:
Hi Pythonistas!
I've got a question about storing tuples in a dictionary. First, a
small test case which creates a list of dictionaries:
import time
list_of_dicts =
keys =
prev_clk = time.clock()
|
by: Assimalyst |
last post by:
Hi
I have a Dictionary<string, List<string>>, which i have successfully
filled. My problem is I need to create a filter expression using all
possible permutations of its contents.
i.e. the dictionary essentially creates the following array:
Key Value
|
by: lee |
last post by:
hi,
i have a dictionary as follows :
kev : {'phno': , 'email': , 'name': ,
'address': }
if user is enters the 3rd item of key phno, ie "dfsdf" in my dict,
how can i find it is the third item in the internal list of phno of
that dictionary? thanks you.
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |