473,385 Members | 1,372 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,385 software developers and data experts.

Adding elements of list into a string

Good day,

I am trying to use a list of integers and strings, take certain elements out, and concatenate them into a string.
i.e.
Expand|Select|Wrap|Line Numbers
  1. lista = [0,'f',0,1,1,1,0,'a',8]
  2. split_length = 3
  3. stringa = ''
  4.  
  5. while split_length >= 0:
    print 'XX ',
    for idx in lista:
    stringa = stringa + str(lista[idx]) + ' ',
    print 'XX'
What I am wanting this to print would be:
XX 0 f 0 XX

What I am getting is:
Expand|Select|Wrap|Line Numbers
  1. XX
  2. Traceback (most recent call last):
    File "<stdin>", line 4 in <module>
    TypeError: list indices must be integers, not str
  3.  
Do I need to convert this into tuples of the length desired, or is there a different way?
Mar 1 '12 #1

✓ answered by Smygis

I'm not sure what's going on there but I think you should look in to the join attribute of strings as well as slicing.

Expand|Select|Wrap|Line Numbers
  1. >>> l=[0,'f',0,1,1,1,0,'a',8]
  2. >>> " ".join([str(i) for i in l[:3]])
  3. '0 f 0'
Its much easier to create a list and then join it together in to a string.

Slicing works as follows sequence[x:y:z] where;
sequence = any list tuple or string
x = the starting element, empty means start of the sequence
y = the end of the slice, empty is the end
z = stepping of the slicing for instance 2 is every other element, not required as it defaults to 1.

example :
Expand|Select|Wrap|Line Numbers
  1. >>> l=[0,'f',0,1,1,1,0,'a',8]
  2. >>> l[::-1] #reversing a list
  3. [8, 'a', 0, 1, 1, 1, 0, 'f', 0]
  4. >>> l[1:-1:2] #removing the first, last and every other element
  5. ['f', 1, 1, 'a']

1 1842
Smygis
126 100+
I'm not sure what's going on there but I think you should look in to the join attribute of strings as well as slicing.

Expand|Select|Wrap|Line Numbers
  1. >>> l=[0,'f',0,1,1,1,0,'a',8]
  2. >>> " ".join([str(i) for i in l[:3]])
  3. '0 f 0'
Its much easier to create a list and then join it together in to a string.

Slicing works as follows sequence[x:y:z] where;
sequence = any list tuple or string
x = the starting element, empty means start of the sequence
y = the end of the slice, empty is the end
z = stepping of the slicing for instance 2 is every other element, not required as it defaults to 1.

example :
Expand|Select|Wrap|Line Numbers
  1. >>> l=[0,'f',0,1,1,1,0,'a',8]
  2. >>> l[::-1] #reversing a list
  3. [8, 'a', 0, 1, 1, 1, 0, 'f', 0]
  4. >>> l[1:-1:2] #removing the first, last and every other element
  5. ['f', 1, 1, 'a']
Mar 1 '12 #2

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

Similar topics

8
by: JS | last post by:
I am trying to add an element to an array like this: var ty = ; zz = "ty"; var ab = null; zz+=""; ab = eval(zz); document.write(ab.length);
0
by: lgdev | last post by:
I am using a Dataset with two tables (Customer, Order) and adding a DataRelation object with Nested property set to True. I do not have a schema defined for the dataset. Calling GetXml results in...
1
by: John | last post by:
Hi How can I programmatically add elements to a listbox? Thanks Regards
5
by: yawnmoth | last post by:
I'm having some difficulty with adding elements to a webpage via the DOM. The following works: main.htm: <script> js = document.createElement('script'); js.src='test.js';...
1
by: Joe Van Dyk | last post by:
#include <string> #include <vector> #include <iostream> class Element { public: Element(std::string n) : name_(n) {}; std::string serialize() const {
3
by: Frances | last post by:
how to do you add elements to an array? am looking at section dealing with arrays in JS Bible, can't find how you add to an array.. in the last few years I've been doing much more Java than JS, am...
4
by: alf | last post by:
Hi, Would it be .append()? Does it reallocate te list with each apend? l= for i in xrange(n): l.append(i) Thx, A.
4
by: dheerajsah | last post by:
hi all, I have created a DIV tag using DOM ..... Element div = DOM.createDiv(); and appended Label inside it. .... Element label = DOM.createLabel(); DOM.setAttribute(label, "value",...
1
by: Aur_Ros | last post by:
I am trying to get a string that contains the HTML code generated for a table control, Need help, examples about how to use render method and string builder, Thanks !!!
6
by: santiago | last post by:
I guess one cannot do this: arraytot = arraytot + arraydet; So, what's the trick to adding arrays like this? Thanks.
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.