top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

split in python without using comma [CLOSED]

+1 vote
303 views

How can I use the '.split()' method (am I right in calling it a method?) without instead of writing each comma between words in the pie list in the following code? Also, is there a way to use .split instead of typing the apostrophes?

import random
pie=['keylime', 'peach', 'apple', 'cherry', 'pecan']
print(random.choice(pie))
closed with the note: None
posted Aug 14, 2013 by Seema Siddique

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

I think you are referring to this:

 pie = 'keylime peach apple cherry pecan'.split()

While it's easier to type, and does save a few characters, I think the original list is clearer to a reader of your program.

answer Aug 14, 2013 by Abhay Kulkarni
Similar Questions
0 votes

I want to use python for creating dynamic database driven websites. and I don't want to use existing web frameworks for my work. I am learning things so I wont feel lazy to write all the code myself because I want to learn.

could anyone suggest me any books/site from where I can start. I want to follow MVC architecture. so please suggest me some links/book or anything

0 votes

I have a list, songs, which I want to divide into two groups.
Essentially, I want:

new_songs = [s for s in songs if s.is_new()]
old_songs = [s for s in songs if not s.is_new()]

but I don't want to make two passes over the list. I could do:

new_songs = []
old_songs = []
for s in songs:
 if s.is_new():
 new_songs.append(s)
 else:
 old_songs.append(s)

Which works, but is klunky compared to the two-liner above. This seems like a common enough thing that I was expecting to find something in itertools which did this. I'm thinking something along the lines of:

matches, non_matches = isplit(lambda s: s.is_new, songs)

Does such a thing exist?

+1 vote

The source code:

for i in range(8):
 n = input()

When we run it, consider the numbers below is the user input,

1
2
3
(and so forth)

my question, can i make it in just a single line like,

1 2 3 (and so forth)
...