Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23218

How can i write for&if-else statement using only one line?

$
0
0
  • Make List a String: Let's make the list ["Life", "is", "too", "short"] into a "Life is too short" string and print it out.

First, Let me tell you i know the way to solve the problem using join() method.

I wanted to solve this using another method, and i used for statement as below.

liszt = ['Life', 'is', 'too', 'short']restr = ''for i in liszt: restr += i+'' if liszt.index(i) != 3 else restr += iprint(restr)

How can i correct this in valid syntax?or... is there any simpler way to code this than mine?

At that time, I intended to express same thing as below using one line. But editor told me it's invalid syntax.

liszt = ['Life', 'is', 'too', 'short']restr = ''for i in liszt:    if liszt.index(i) != 3:        restr += i+''    else:        restr += iprint(restr)

Viewing all articles
Browse latest Browse all 23218

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>