Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9010939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:35:44+00:00 2026-06-16T02:35:44+00:00

I have a list (array) with mixed a = [x, 2, y] b =

  • 0

I have a list (array) with mixed

a = ["x", "2", "y"]
b = ["x", 2, "y"]
print ":".join(a)
print ":".join(b)

The first join works, but the second one throws a TypeError exception

I came up with this, but is this the Python solution?

print ":".join(map(str, b))

BTW in the end I just would like to write this string to a file, so if there is a specific solution for that, I’d appreciate that too.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T02:35:46+00:00Added an answer on June 16, 2026 at 2:35 am

    Your solution works nicely and is probably one of the fastest ways to do this for small to medium sized lists, but it creates an unnecessary list (in python2.x). Usually that’s not a problem, but in a few cases, depending on the object b, it could be an issue. Another which is lazy in python2 as well as python 3 is:

    ':'.join(str(x) for x in b)
    

    Some timings for python 2.7.3:

    $ python -m timeit -s 'b = ["x", 2, "y"]' '":".join(map(str,b))'
    1000000 loops, best of 3: 1.66 usec per loop
    $python -m timeit -s 'b = ["x", 2, "y"]' '":".join([str(x) for x in b])'
    1000000 loops, best of 3: 1.49 usec per loop
    $ python -m timeit -s 'b = ["x", 2, "y"]' '":".join(str(x) for x in b)'
    100000 loops, best of 3: 3.26 usec per loop
    $python -m timeit -s 'from itertools import imap; b = ["x", 2, "y"]' '":".join(imap(str,b))'
    100000 loops, best of 3: 2.83 usec per loop
    

    Some timings for python3.2:

    $ python3 -m timeit -s 'b = ["x", 2, "y"]' '":".join(map(str,b))'
    100000 loops, best of 3: 2.6 usec per loop
    $ python3 -m timeit -s 'b = ["x", 2, "y"]' '":".join([str(x) for x in b])'
    100000 loops, best of 3: 2.08 usec per loop
    $ python3 -m timeit -s 'b = ["x", 2, "y"]' '":".join(str(x) for x in b)'
    100000 loops, best of 3: 3.39 usec per loop
    

    Note that if you let the loop get a lot bigger, the differences become less important:

    python2.7.3:

    $ python -m timeit -s 'b = list(range(10000))' '":".join(str(x) for x in b)'
    100 loops, best of 3: 4.83 msec per loop
    $ python -m timeit -s 'b = list(range(10000))' '":".join([str(x) for x in b])'
    100 loops, best of 3: 4.33 msec per loop
    $ python -m timeit -s 'b = list(range(10000))' '":".join(map(str,b))'
    100 loops, best of 3: 3.29 msec per loop
    

    python 3.2.0

    $ python3 -m timeit -s 'b = list(range(10000))' '":".join(str(x) for x in b)'
    100 loops, best of 3: 6.42 msec per loop
    $ python3 -m timeit -s 'b = list(range(10000))' '":".join([str(x) for x in b])'
    100 loops, best of 3: 5.51 msec per loop
    $ python3 -m timeit -s 'b = list(range(10000))' '":".join(map(str,b))'
    100 loops, best of 3: 4.55 msec per loop
    

    *all timings done on my MacbookPro, OS-X 10.5.8 intel core2duo ….

    Notes,

    • python2.x is faster than python3.x in all cases (for me)
    • List-comprehension turns out to be fastest for your example list, but map is faster for a larger list. map is probably slower for the small list as you need to look up the function whereas the list comprehension cannot be “shadowed”, so no lookup needs to be performed. There may be another turn-around point for HUGE lists where the time it takes to build the intermediate list becomes significant.
    • generator expression is always the slowest (but lazy in both cases)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list/array and need to process certain elements, but also need the
I have a array list which have a list of a type, but all
I have 3 Array list one is for value one is for date and
I have a array list with large collection, and i have one input string.
I have a List with array of object values. I have to display value
I have a string like this: TEST.DATA.Data.COR.Point,2;TEST.DATA.Data.COR.Point,5;TEST.DATA.Data.COR.Point,12;TEST.DATA.Data.COR.Point,12;TEST.DATA.Data.COR.WordTOFIND,18 I have a list of array with
I have an array list of objects in my application. private static ArrayList<Player> userList=new
I have an array list named newSymptomList which contains a list of Symptom id's
I have an array list of objects and I am using this example to
I have an array of strings var controlsToGet = new[] {lblHome,lblContact}; I have List<LanguageControl>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.