Jump to content

Seemingly Simple Python Question [ANSWERED]

Go to solution Solved by fizzlesticks,

If the items in the list are strings you can do

print ''.join(a)

or if they're not strings

print ''.join((str(x) for x in a))

This will also be faster than using a loop and printing each item.

Hello,

So I've made this Python program and it works great. But now I'm converting it to 2.X (I wrote it in 3.4) and I'm having trouble getting the same output in 2.X as 3.4. Basically I'm trying to print out every element in a list without spaces or extra list stuff. In 3.4 it goes like this:

for k in endingList:
    print(k, end="")

And in 2.X I'm doing:

for k in endingList:
    print k,

But in that I get spaces in between the elements, although they are on the same line. I don't know how to get the end="" parameter into 2.X (I'm assuming that's all I have to do, is get it in there somehow) but I can't seem to figure it out. I have a feeling there's a very simple solution I should know, but I can't find it. Thanks for your help in advance

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to comment
https://linustechtips.com/topic/584493-seemingly-simple-python-question-answered/
Share on other sites

Link to post
Share on other sites

printing in 2 is a mess in my opinion, but achievable none the less 

If its newer than 2.6 I think you can import some functions of 3 like the end parameter

There's a module you can import thats supposed to help with printing 

http://stackoverflow.com/questions/493386/how-to-print-in-python-without-newline-or-space

Other than that my only thought is to treat it as a string and edit it before print/echo

 

                     .
                   _/ V\
                  / /  /
                <<    |
                ,/    ]
              ,/      ]
            ,/        |
           /    \  \ /
          /      | | |
    ______|   __/_/| |
   /_______\______}\__}  

Spoiler

[I5-12600k | 32gb DDR5 6000 | RTX5070 | 2x1tb M.2]

 

[Ryzen 5 1600 | 16gb DDR4 3200 | GTX1030 | 4x 8tb HDD] 

 

Link to post
Share on other sites

It should be " " instead of "".

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

8 hours ago, Nineshadow said:

It should be " " instead of "".

But that's the idea, I don't want that space.

 

11 hours ago, RedWulf said:

printing in 2 is a mess in my opinion, but achievable none the less 

If its newer than 2.6 I think you can import some functions of 3 like the end parameter

There's a module you can import thats supposed to help with printing 

http://stackoverflow.com/questions/493386/how-to-print-in-python-without-newline-or-space

Other than that my only thought is to treat it as a string and edit it before print/echo

 

I'll look into that, thanks.

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to post
Share on other sites

If the items in the list are strings you can do

print ''.join(a)

or if they're not strings

print ''.join((str(x) for x in a))

This will also be faster than using a loop and printing each item.

1474412270.2748842

Link to post
Share on other sites

This works for me :

list={1,2,3,4,5,6,7,8,9,10}
for i in list:
	print(i,end="")

It prints the numbers without any spaces.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

3 hours ago, fizzlesticks said:

If the items in the list are strings you can do


print ''.join(a)

or if they're not strings


print ''.join((str(x) for x in a))

This will also be faster than using a loop and printing each item.

Thank you! This is what I needed.

14 minutes ago, Nineshadow said:

This works for me :


list={1,2,3,4,5,6,7,8,9,10}
for i in list:
	print(i,end="")

It prints the numbers without any spaces.

I believe you're in Python 3.X, and yes, there it works. But I'm converting my 3.X program to 2.X and print parentheses are invalid syntax.

Join the Appleitionist cause! See spoiler below for answers to common questions that shouldn't be common!

Spoiler

Q: Do I have a virus?!
A: If you didn't click a sketchy email, haven't left your computer physically open to attack, haven't downloaded anything sketchy/free, know that your software hasn't been exploited in a new hack, then the answer is: probably not.

 

Q: What email/VPN should I use?
A: Proton mail and VPN are the best for email and VPNs respectively. (They're free in a good way)

 

Q: How can I stay anonymous on the (deep/dark) webzz???....

A: By learning how to de-anonymize everyone else; if you can do that, then you know what to do for yourself.

 

Q: What Linux distro is best for x y z?

A: Lubuntu for things with little processing power, Ubuntu for normal PCs, and if you need to do anything else then it's best if you do the research yourself.

 

Q: Why is my Linux giving me x y z error?

A: Have you not googled it? Are you sure StackOverflow doesn't have an answer? Does the error tell you what's wrong? If the answer is no to all of those, message me.

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×