473,406 Members | 2,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

removing quotes from a list print out

Thekid
145 100+
How can you take a list and get rid of the quotes when it prints out?
if you have this:
>>list = ["123", "456", "789"]
>>print list
['123','456','789']

but want it to print out like this:
[123,456,789]
or just
123,456,789

I tried .split or .replace but it says list has no attribute split or replace
Oct 10 '08 #1
5 65932
bvdet
2,851 Expert Mod 2GB
Here's some options:
Expand|Select|Wrap|Line Numbers
  1. >>> lst = ["123", "456", "789"]
  2. >>> print ','.join(lst)
  3. 123,456,789
  4. >>> print "[%s]" % (','.join(lst))
  5. [123,456,789]
  6. >>> print [int(i) for i in lst]
  7. [123, 456, 789]
  8. >>> 
Oct 10 '08 #2
Thekid
145 100+
Those work great, thanks. One more question...what if the list contains something other than integers as well but I just need the integers without quotes? Example:

lst = ["hat", "3", "5", "8"]
["hat", 3, 5, 8]

I tried the last suggestion you posted with [int ( i ) for i in lst] but it gives an error message.
Oct 10 '08 #3
boxfish
469 Expert 256MB
I'm sure bvdet can give you something better, but
Expand|Select|Wrap|Line Numbers
  1. lst = ["hat", "3", "5", "8"]
  2. lst_4_print = []
  3. for i in lst:
  4.     try:
  5.         lst_4_print.append(int(i))
  6.     except ValueError:
  7.         lst_4_print.append(i)
  8. print lst_4_print
  9.  
Oct 10 '08 #4
bvdet
2,851 Expert Mod 2GB
I'm sure bvdet can give you something better, but
Expand|Select|Wrap|Line Numbers
  1. lst = ["hat", "3", "5", "8"]
  2. lst_4_print = []
  3. for i in lst:
  4.     try:
  5.         lst_4_print.append(int(i))
  6.     except ValueError:
  7.         lst_4_print.append(i)
  8. print lst_4_print
  9.  
This is not necessarily better, just slightly different:
Expand|Select|Wrap|Line Numbers
  1. >>> lst = ["hat", "3", "5", "8"]
  2. >>> lst_4_print = []
  3. >>> for i in lst:
  4. ...     if i.isdigit():
  5. ...         lst_4_print.append(int(i))
  6. ...     else:
  7. ...         lst_4_print.append(i)
  8. ...         
  9. >>> lst_4_print
  10. ['hat', 3, 5, 8]
  11. >>> 
Oct 10 '08 #5
Thekid
145 100+
Thank you both for your suggestions!
Oct 10 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

24
by: deko | last post by:
I'm trying to log error messages and sometimes (no telling when or where) the message contains a string with double quotes. Is there a way get the query to insert the string with the double...
24
by: RyanTaylor | last post by:
I have a final coming up later this week in my beginning Java class and my prof has decided to give us possible Javascript code we may have to write. Problem is, we didn't really cover JS and what...
7
by: Miguel E. | last post by:
Hi, I've been (self) studying Python for the past two months and I have had no background in OOP whatsoever. I was able to write an interactive program that randomly selects an item from a...
5
by: nuffnough | last post by:
This is python 2.4.3 on WinXP under PythonWin. I have a config file with many blank lines and many other lines that I don't need. read the file in, splitlines to make a list, then run a loop...
6
by: bruce | last post by:
hi... i'm running into a problem where i'm seeing non-ascii chars in the parsing i'm doing. in looking through various docs, i can't find functions to remove/restrict strings to valid ascii...
17
by: Eric_Dexter | last post by:
def simplecsdtoorc(filename): file = open(filename,"r") alllines = file.read_until("</CsInstruments>") pattern1 = re.compile("</") orcfilename = filename + "orc" for line in alllines: if not...
0
NeoPa
by: NeoPa | last post by:
Background Whenever code is used there must be a way to differentiate the actual code (which should be interpreted directly) with literal strings which should be interpreted as data. Numbers don't...
4
by: Michael Yanowitz | last post by:
Hello: If I have a long string (such as a Python file). I search for a sub-string in that string and find it. Is there a way to determine if that found sub-string is inside single-quotes or...
3
m6s
by: m6s | last post by:
Hello to all, I am having trouble removing an item from list, which the item is a list by itself. <code> for self.line in self.filtered: if self.appid: if self.line <> self.appid: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.