Lists [ HELP ]

A forum for general discussion of the Python programming language.

Lists [ HELP ]

Postby GabrielSilva » Mon May 20, 2013 12:26 am

def cool(x):
c = 0
while c < x:
print "hello word"
c = c + 1
cool(2)
Result:
"hello word"
"hello word"

Question: How can I transform this, into a list ? like ["hello word", "hello word"]
GabrielSilva
 
Posts: 8
Joined: Mon May 20, 2013 12:20 am

Re: Lists [ HELP ]

Postby metulburr » Mon May 20, 2013 12:27 am

New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1103
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Lists [ HELP ]

Postby GabrielSilva » Mon May 20, 2013 12:29 am

Could you give me an example?
GabrielSilva
 
Posts: 8
Joined: Mon May 20, 2013 12:20 am

Re: Lists [ HELP ]

Postby metulburr » Mon May 20, 2013 12:31 am

I could, but I won't, not until you use code tags
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1103
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Lists [ HELP ]

Postby GabrielSilva » Mon May 20, 2013 12:41 am

You're a good professor.
GabrielSilva
 
Posts: 8
Joined: Mon May 20, 2013 12:20 am

Re: Lists [ HELP ]

Postby GabrielSilva » Mon May 20, 2013 1:07 am

Code: Select all
def cool(x):
   c = 0
   while c < x:
         print "hello word"
         c = c + 1

cool(2)
Result:
"hello word"
"hello word"



What I want is ["hello word", "hello word"]

And, sorry for the misunderstand.
GabrielSilva
 
Posts: 8
Joined: Mon May 20, 2013 12:20 am

Re: Lists [ HELP ]

Postby metulburr » Mon May 20, 2013 2:38 am

What I want is ["hello word", "hello word"]

instead of printing the string 'hello world', just append that string to a list, with the append() method
New Users, Read This
version Python 3.3.2 and 2.7.5, tkinter 8.5, pyqt 4.8.4, pygame 1.9.2 pre
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
User avatar
metulburr
 
Posts: 1103
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Lists [ HELP ]

Postby snippsat » Mon May 20, 2013 2:48 am

Code: Select all
def cool(x):
    lst = []
    c = 0
    while c < x:
        s = "hello word"
        lst.append(s)
        c = c + 1
    return lst

def cool1(x):
    '''A better way'''
    s = "hello word"
    return [s for i in range(x)]

def cool2(times, string):
    '''A better way with arguments'''
    return [string for i in range(times)]

Test.
Code: Select all
>>> cool2(5, 'hello world')
['hello world', 'hello world', 'hello world', 'hello world', 'hello world']
User avatar
snippsat
 
Posts: 88
Joined: Thu Feb 21, 2013 12:04 am

Re: Lists [ HELP ]

Postby Yoriz » Mon May 20, 2013 5:35 am

Code: Select all
cool = ['hello world'] * 2
print cool

['hello world', 'hello world']
New Users, Read This
Join the #python-forum IRC channel on irc.freenode.net!
Spam topic disapproval technician
Windows7, Python 2.7.4., WxPython 2.9.5.0., some Python 3.3
User avatar
Yoriz
 
Posts: 565
Joined: Fri Feb 08, 2013 1:35 am
Location: UK


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests