Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #86230 > unrolled thread

Concatenate list values

Started byloial <jldunn2000@gmail.com>
First post2015-02-23 07:58 -0800
Last post2015-02-24 00:37 -0800
Articles 9 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  Concatenate list values loial <jldunn2000@gmail.com> - 2015-02-23 07:58 -0800
    Re: Concatenate list values John Gordon <gordon@panix.com> - 2015-02-23 16:07 +0000
    Re: Concatenate list values alister <alister.nospam.ware@ntlworld.com> - 2015-02-23 16:08 +0000
    Re: Concatenate list values Peter Otten <__peter__@web.de> - 2015-02-23 17:09 +0100
    Re: Concatenate list values Tim Chase <python.list@tim.thechases.com> - 2015-02-23 10:14 -0600
    Re: Concatenate list values Andrew Berg <aberg010@my.hennepintech.edu> - 2015-02-23 10:14 -0600
    Re: Concatenate list values Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-02-23 17:13 +0000
      Re: Concatenate list values wxjmfauth@gmail.com - 2015-02-24 02:30 -0800
    Re: Concatenate list values loial <jldunn2000@gmail.com> - 2015-02-24 00:37 -0800

#86230 — Concatenate list values

Fromloial <jldunn2000@gmail.com>
Date2015-02-23 07:58 -0800
SubjectConcatenate list values
Message-ID<12821378-62af-4954-8b61-aa0738c5ff5c@googlegroups.com>
Is there a quick way to concatenate all the values in a list into a string, except the first value?

I want this to work with variable length lists.

All values in list will be strings.

Any help appreciated

[toc] | [next] | [standalone]


#86231

FromJohn Gordon <gordon@panix.com>
Date2015-02-23 16:07 +0000
Message-ID<mcfj76$d2c$1@reader1.panix.com>
In reply to#86230
In <12821378-62af-4954-8b61-aa0738c5ff5c@googlegroups.com> loial <jldunn2000@gmail.com> writes:

> Is there a quick way to concatenate all the values in a list into a string, except the first value?

> I want this to work with variable length lists.

> All values in list will be strings.

> Any help appreciated

big_string = ', '.join(my_list[1:])

-- 
John Gordon         Imagine what it must be like for a real medical doctor to
gordon@panix.com    watch 'House', or a real serial killer to watch 'Dexter'.

[toc] | [prev] | [next] | [standalone]


#86232

Fromalister <alister.nospam.ware@ntlworld.com>
Date2015-02-23 16:08 +0000
Message-ID<mcfj9v$8lf$1@speranza.aioe.org>
In reply to#86230
On Mon, 23 Feb 2015 07:58:39 -0800, loial wrote:

> Is there a quick way to concatenate all the values in a list into a
> string, except the first value?
> 
> I want this to work with variable length lists.
> 
> All values in list will be strings.
> 
> Any help appreciated

''.join(mylist[1:])

all elements must be strings (as specified) or it will cause an error




-- 
petribar:
	Any sun-bleached prehistoric candy that has been sitting in
	the window of a vending machine too long.
		-- Rich Hall, "Sniglets"

[toc] | [prev] | [next] | [standalone]


#86233

FromPeter Otten <__peter__@web.de>
Date2015-02-23 17:09 +0100
Message-ID<mailman.19078.1424707801.18130.python-list@python.org>
In reply to#86230
loial wrote:

> Is there a quick way to concatenate all the values in a list into a
> string, except the first value?
> 
> I want this to work with variable length lists.
> 
> All values in list will be strings.
> 
> Any help appreciated

>>> strings
['All', 'values', 'in', 'list', 'will', 'be', 'strings']

Build a new list without the first item:

>>> strings[1:]
['values', 'in', 'list', 'will', 'be', 'strings']

Concatenate the strings:

>>> "|".join(strings[1:])
'values|in|list|will|be|strings'

If you don't want a separator invoke the join method on an empty string:

>>> "".join(strings[1:])
'valuesinlistwillbestrings'

[toc] | [prev] | [next] | [standalone]


#86234

FromTim Chase <python.list@tim.thechases.com>
Date2015-02-23 10:14 -0600
Message-ID<mailman.19079.1424707997.18130.python-list@python.org>
In reply to#86230
On 2015-02-23 07:58, loial wrote:
> Is there a quick way to concatenate all the values in a list into a
> string, except the first value?
> 
> I want this to work with variable length lists.
> 
> All values in list will be strings.

Using

  "".join(my_list[1:])

should work.

If it is an arbitrary iterable instead of a list (and thus can't be
sliced), you can use

  "".join(itertools.islice(my_iter, 1, None))

-tkc




[toc] | [prev] | [next] | [standalone]


#86237

FromAndrew Berg <aberg010@my.hennepintech.edu>
Date2015-02-23 10:14 -0600
Message-ID<mailman.19082.1424708919.18130.python-list@python.org>
In reply to#86230
On 2015.02.23 09:58, loial wrote:
> Is there a quick way to concatenate all the values in a list into a string, except the first value?
> 
> I want this to work with variable length lists.
> 
> All values in list will be strings.
> 
> Any help appreciated
> 
The tutorial covers strings and lists:
https://docs.python.org/3/tutorial/introduction.html#strings
And there is ample reference documentation for strings and lists:
https://docs.python.org/3/library/stdtypes.html#string-methods
https://docs.python.org/3/library/string.html
https://docs.python.org/3/library/stdtypes.html#sequence-types-list-tuple-range

Get to know strings and lists, and this should be a trivial problem to solve.

Or just copy/paste the answers others gave and be confused the next time you
need to solve a simple problem.

[toc] | [prev] | [next] | [standalone]


#86240

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-02-23 17:13 +0000
Message-ID<mailman.19084.1424711656.18130.python-list@python.org>
In reply to#86230
On 23/02/2015 16:14, Tim Chase wrote:
> On 2015-02-23 07:58, loial wrote:
>> Is there a quick way to concatenate all the values in a list into a
>> string, except the first value?
>>
>> I want this to work with variable length lists.
>>
>> All values in list will be strings.
>
> Using
>
>    "".join(my_list[1:])
>
> should work.
>
> If it is an arbitrary iterable instead of a list (and thus can't be
> sliced), you can use
>
>    "".join(itertools.islice(my_iter, 1, None))
>
> -tkc
>

Presumably rather more efficient for long lists as you're not taking a 
copy of (effectively) the entire list.  Any volunteers to measure it, 
not mentioning any names, Mr D'Aprano? :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#86308

Fromwxjmfauth@gmail.com
Date2015-02-24 02:30 -0800
Message-ID<a2820547-b159-4968-a922-93be8f189839@googlegroups.com>
In reply to#86240
Le lundi 23 février 2015 18:15:11 UTC+1, Mark Lawrence a écrit :
> On 23/02/2015 16:14, Tim Chase wrote:
> > On 2015-02-23 07:58, loial wrote:
> >> Is there a quick way to concatenate all the values in a list into a
> >> string, except the first value?
> >>
> >> I want this to work with variable length lists.
> >>
> >> All values in list will be strings.
> >
> > Using
> >
> >    "".join(my_list[1:])
> >
> > should work.
> >
> > If it is an arbitrary iterable instead of a list (and thus can't be
> > sliced), you can use
> >
> >    "".join(itertools.islice(my_iter, 1, None))
> >
> > -tkc
> >
> 
> Presumably rather more efficient for long lists as you're not taking a 
> copy of (effectively) the entire list.  Any volunteers to measure it, 
> not mentioning any names, Mr D'Aprano? :)
> 
> -- 

>>> timeit.repeat("'a'.join(['a']*123)")
[2.747081951123846, 2.7309830094075096, 2.7286731458373197]
>>> timeit.repeat("'\u1234'.join(['a']*123)")
[2.7187153298930298, 2.7063901499382155, 2.709908310428318]
>>> 


>>> timeit.repeat("'a'.join(['a']*123)")
[2.935619986680422, 2.931890334845548, 2.9307190587571768]
>>> timeit.repeat("'\u1234'.join(['a']*123)")
[6.233187127305428, 6.218316962117797, 6.228552434296944]
>>>

What is wrong by design will always stays wrong.
(On top of this, it's buggy)

jmf

[toc] | [prev] | [next] | [standalone]


#86307

Fromloial <jldunn2000@gmail.com>
Date2015-02-24 00:37 -0800
Message-ID<3ee01d6c-3a79-4f25-b425-0ce5b4b178f1@googlegroups.com>
In reply to#86230
Many thanks for those you chose to help me out. Problem solved.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web