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 1814
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.
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]
-
>>>
Sweet, thanks. Much better!
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]
-
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()) #...
|
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...
|
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...
|
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...
|
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...
|
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 =...
|
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...
|
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...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
| |