Stop learning alone!

Learn faster and stay on-track by joining this free class with other self-learners.

Register for MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming now.

MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming

Class length: 24 weeks. Start anytime.

Creator: duallain

Status: Established

Join this class!

list vs tuples

when an assignment asks for a tuple to be returned, is it better to work with the data as a list and then change it to a tuple when it is returned... Rather than work with the data as a tuple the whole time?

list.append(x) vs tuple += (x,) <- this keeps creating new tuples, does this affect memory or performance?

tuckertuck
10 months ago

Reply


I also had some problems with tuples. When I tried to concatenate an integer with a tuple, the compiler gave me error, but when I tried it for lists it worked.

This was my code:

a = (1,2,3) a (1, 2, 3) i = 4 i 4 a = a + (i)

Traceback (most recent call last): File "", line 1, in a = a + (i) TypeError: can only concatenate tuple (not "int") to tuple

annna
10 months ago

Reply

* Markdown Cheatsheet:

Link:
[clickable text](http://www.example.com)

New Paragraph:
Hit enter twice

Main heading:
# Main Heading Text

Sub-heading:
## Sub-heading Text

List:
* item 1
* item 2
* item 3

Italics:
*italicized text*

Bold:
**bold text**

YouTube:
URL (http://www.youtube.com/watch?v=Ui4AYPcRkYE) turns into embed code

Full Markdown reference