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 1829
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: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |