Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #34184 > unrolled thread
| Started by | subhabangalore@gmail.com |
|---|---|
| First post | 2012-12-03 11:58 -0800 |
| Last post | 2012-12-04 13:54 +0000 |
| Articles | 13 — 11 participants |
Back to article view | Back to comp.lang.python
Conversion of List of Tuples subhabangalore@gmail.com - 2012-12-03 11:58 -0800
Re: Conversion of List of Tuples John Gordon <gordon@panix.com> - 2012-12-03 20:04 +0000
Re: Conversion of List of Tuples MRAB <python@mrabarnett.plus.com> - 2012-12-03 20:13 +0000
Re: Conversion of List of Tuples Chris Angelico <rosuav@gmail.com> - 2012-12-04 07:17 +1100
Re: Conversion of List of Tuples Chris Kaynor <ckaynor@zindagigames.com> - 2012-12-03 12:10 -0800
Re: Conversion of List of Tuples subhabangalore@gmail.com - 2012-12-03 13:14 -0800
Re: Conversion of List of Tuples John Gordon <gordon@panix.com> - 2012-12-03 22:02 +0000
Re: Conversion of List of Tuples Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-12-03 22:11 +0000
Re: Conversion of List of Tuples Walter Hurry <walterhurry@lavabit.com> - 2012-12-03 22:43 +0000
Re: Conversion of List of Tuples Gary Herron <gherron@digipen.edu> - 2012-12-03 12:08 -0800
Re: Conversion of List of Tuples Alexander Blinne <news@blinne.net> - 2012-12-04 10:44 +0100
Re: Conversion of List of Tuples Hans Mulder <hansmu@xs4all.nl> - 2012-12-04 12:45 +0100
Re: Conversion of List of Tuples Neil Cerutti <neilc@norwich.edu> - 2012-12-04 13:54 +0000
| From | subhabangalore@gmail.com |
|---|---|
| Date | 2012-12-03 11:58 -0800 |
| Subject | Conversion of List of Tuples |
| Message-ID | <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> |
Dear Group, I have a tuple of list as, tup_list=[(1,2), (3,4)] Now if I want to covert as a simple list, list=[1,2,3,4] how may I do that? If any one can kindly suggest? Googling didn't help much. Regards, Subhabrata.
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2012-12-03 20:04 +0000 |
| Message-ID | <k9j0k6$e4l$1@reader1.panix.com> |
| In reply to | #34184 |
In <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> subhabangalore@gmail.com writes:
> Dear Group,
> I have a tuple of list as,
> tup_list=[(1,2), (3,4)]
> Now if I want to covert as a simple list,
> list=[1,2,3,4]
> how may I do that?
new_list = []
for t in tup_list:
for item in t:
new_list.append(item)
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | MRAB <python@mrabarnett.plus.com> |
|---|---|
| Date | 2012-12-03 20:13 +0000 |
| Message-ID | <mailman.438.1354565580.29569.python-list@python.org> |
| In reply to | #34185 |
On 2012-12-03 20:04, John Gordon wrote:
> In <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> subhabangalore@gmail.com writes:
>
>> Dear Group,
>
>> I have a tuple of list as,
>
>> tup_list=[(1,2), (3,4)]
>> Now if I want to covert as a simple list,
>
>> list=[1,2,3,4]
>
>> how may I do that?
>
> new_list = []
>
> for t in tup_list:
> for item in t:
> new_list.append(item)
>
Or you could use .extend:
new_list = []
for t in tup_list:
new_list.extend(t)
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2012-12-04 07:17 +1100 |
| Message-ID | <mailman.439.1354565829.29569.python-list@python.org> |
| In reply to | #34185 |
On Tue, Dec 4, 2012 at 7:04 AM, John Gordon <gordon@panix.com> wrote:
> In <6049bc85-6f8e-429b-a855-ecef47a9d28e@googlegroups.com> subhabangalore@gmail.com writes:
>
>> Dear Group,
>
>> I have a tuple of list as,
>
>> tup_list=[(1,2), (3,4)]
>> Now if I want to covert as a simple list,
>
>> list=[1,2,3,4]
>
>> how may I do that?
>
> new_list = []
>
> for t in tup_list:
> for item in t:
> new_list.append(item)
Which can be written more succintly as:
new_list = []
for t in tup_list:
new_list.extend(t)
In more general terms, what you're looking to do here is *flatten*
your structure. Not sure if that would have helped in the web search
that you doubtless did before asking this question. :)
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Chris Kaynor <ckaynor@zindagigames.com> |
|---|---|
| Date | 2012-12-03 12:10 -0800 |
| Message-ID | <mailman.437.1354565427.29569.python-list@python.org> |
| In reply to | #34184 |
On Mon, Dec 3, 2012 at 11:58 AM, <subhabangalore@gmail.com> wrote: > Dear Group, > > I have a tuple of list as, > > tup_list=[(1,2), (3,4)] > Now if I want to covert as a simple list, > > list=[1,2,3,4] > > how may I do that? > > If any one can kindly suggest? Googling didn't help much. If you know they are always exactly two levels deep, you can use nested loops (in comprehension form): [item for tuple_ in list_ for item in tuple_] That could also be written how John recommended, in three lines. > > Regards, > Subhabrata. > -- > http://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | subhabangalore@gmail.com |
|---|---|
| Date | 2012-12-03 13:14 -0800 |
| Message-ID | <e89cddcc-71ab-4223-b66e-8c853634aa8f@googlegroups.com> |
| In reply to | #34184 |
On Tuesday, December 4, 2012 1:28:17 AM UTC+5:30, subhaba...@gmail.com wrote: > Dear Group, > > > > I have a tuple of list as, > > > > tup_list=[(1,2), (3,4)] > > Now if I want to covert as a simple list, > > > > list=[1,2,3,4] > > > > how may I do that? > > > > If any one can kindly suggest? Googling didn't help much. > > > > Regards, > > Subhabrata. Thanks. But I am not getting the counter "5posts 0 views"...if moderator can please check the issue.
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2012-12-03 22:02 +0000 |
| Message-ID | <k9j7hr$6ib$1@reader1.panix.com> |
| In reply to | #34191 |
In <e89cddcc-71ab-4223-b66e-8c853634aa8f@googlegroups.com> subhabangalore@gmail.com writes:
> Thanks. But I am not getting the counter "5posts 0 views"...if
> moderator can please check the issue.
I logged in via Google Groups and all the replies were present. What
is your question?
(This group is not moderated.)
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-12-03 22:11 +0000 |
| Message-ID | <50bd239c$0$29994$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #34191 |
On Mon, 03 Dec 2012 13:14:19 -0800, subhabangalore wrote: > Thanks. But I am not getting the counter "5posts 0 views"...if moderator > can please check the issue. What counter are you talking about? This is an email mailing list, also copied to the Usenet newsgroup comp.lang.python, and mirrored on other places including gmane and various web sites. Neither email nor Usenet include "counters", so you will have to explain what you are talking about. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Walter Hurry <walterhurry@lavabit.com> |
|---|---|
| Date | 2012-12-03 22:43 +0000 |
| Message-ID | <k9j9us$4ip$1@news.albasani.net> |
| In reply to | #34194 |
On Mon, 03 Dec 2012 22:11:40 +0000, Steven D'Aprano wrote: > On Mon, 03 Dec 2012 13:14:19 -0800, subhabangalore wrote: > >> Thanks. But I am not getting the counter "5posts 0 views"...if >> moderator can please check the issue. > > What counter are you talking about? > > This is an email mailing list, also copied to the Usenet newsgroup > comp.lang.python, and mirrored on other places including gmane and > various web sites. Neither email nor Usenet include "counters", so you > will have to explain what you are talking about. Doubtless he is talking about G**gle Groups, since I don't see his posts anyway.
[toc] | [prev] | [next] | [standalone]
| From | Gary Herron <gherron@digipen.edu> |
|---|---|
| Date | 2012-12-03 12:08 -0800 |
| Message-ID | <mailman.452.1354609850.29569.python-list@python.org> |
| In reply to | #34184 |
On 12/03/2012 11:58 AM, subhabangalore@gmail.com wrote: > [(1,2), (3,4)] >>> L=[(1,2), (3,4)] >>> >>> [b for a in L for b in a] [1, 2, 3, 4] -- Dr. Gary Herron Department of Computer Science DigiPen Institute of Technology (425) 895-4418
[toc] | [prev] | [next] | [standalone]
| From | Alexander Blinne <news@blinne.net> |
|---|---|
| Date | 2012-12-04 10:44 +0100 |
| Message-ID | <50bdc601$0$9517$9b4e6d93@newsspool1.arcor-online.net> |
| In reply to | #34184 |
Am 03.12.2012 20:58, schrieb subhabangalore@gmail.com: > Dear Group, > > I have a tuple of list as, > > tup_list=[(1,2), (3,4)] > Now if I want to covert as a simple list, > > list=[1,2,3,4] > > how may I do that? Another approach that has not yet been mentioned here: >>> a=[(1,2), (3,4)] >>> b=[] >>> map(b.extend, a) [None, None] >>> b [1, 2, 3, 4] map returns [None, None] because extend returns nothing, but now b==[1,2,3,4]. There are more ways: >>> from operator import add >>> reduce(add, a) (1, 2, 3, 4) or >>> reduce(operator.add, (list(t) for t in a)) [1, 2, 3, 4] I didn't do any performance testing, i guess the first one should be about as fast es the for-loop approach with .extend() and the other two might be quite slow. Although this only really matters if you have large lists. Greetings
[toc] | [prev] | [next] | [standalone]
| From | Hans Mulder <hansmu@xs4all.nl> |
|---|---|
| Date | 2012-12-04 12:45 +0100 |
| Message-ID | <50bde242$0$6947$e4fe514c@news2.news.xs4all.nl> |
| In reply to | #34212 |
On 4/12/12 10:44:32, Alexander Blinne wrote: > Am 03.12.2012 20:58, schrieb subhabangalore@gmail.com: >> Dear Group, >> >> I have a tuple of list as, >> >> tup_list=[(1,2), (3,4)] >> Now if I want to covert as a simple list, >> >> list=[1,2,3,4] >> >> how may I do that? > > Another approach that has not yet been mentioned here: > >>>> a=[(1,2), (3,4)] >>>> b=[] >>>> map(b.extend, a) > [None, None] >>>> b > [1, 2, 3, 4] > > map returns [None, None] because extend returns nothing, but now > b==[1,2,3,4]. It's considered bad style to use map it you don't want the list it produces. > There are more ways: > >>>> from operator import add >>>> reduce(add, a) > (1, 2, 3, 4) There's a built-in that does "reduce(operator.add"; it's called "sum": >>> sum(a, ()) (1, 2, 3, 4) >>> > or > >>>> reduce(operator.add, (list(t) for t in a)) > [1, 2, 3, 4] This is a valid use case for the map operator: >>> sum(map(list, a), []) [1, 2, 3, 4] >>> > I didn't do any performance testing, i guess the first one should be > about as fast as the for-loop approach with .extend() and the other two > might be quite slow. Although this only really matters if you have large > lists. Hope this helps, -- HansM
[toc] | [prev] | [next] | [standalone]
| From | Neil Cerutti <neilc@norwich.edu> |
|---|---|
| Date | 2012-12-04 13:54 +0000 |
| Message-ID | <ai6dkvFk3vkU4@mid.individual.net> |
| In reply to | #34219 |
On 2012-12-04, Hans Mulder <hansmu@xs4all.nl> wrote:
> It's considered bad style to use map it you don't want the list it
> produces.
>
>> There are more ways:
>>
>>>>> from operator import add
>>>>> reduce(add, a)
>> (1, 2, 3, 4)
>
> There's a built-in that does "reduce(operator.add"; it's called "sum":
>
>>>> sum(a, ())
> (1, 2, 3, 4)
I thought that sort of thing would cause a warning. Maybe it's
only for lists.
Here's the recipe from the itertools documentation:
def flatten(listOfLists):
"Flatten one level of nesting"
return chain.from_iterable(listOfLists)
--
Neil Cerutti
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web