27

I am wondering how to put a tuple into an array? or is it better to use arrays in array for the design of the program rather than a tuple in an array? please advice me. thank you

4
  • 3
    Do you mean to make an array of tuples? Or to make a tuple into an array? Commented Aug 14, 2011 at 15:53
  • 5
    Do you mean list on an actual array? Commented Aug 14, 2011 at 15:56
  • i just want to store value like text and ID into tuple or list but i want to check it with another for loop. instead of using a for loop in a for loop. i am thinking to use arrays in array. But was pretty confuse whether should i use tuples in list or lists in a list. I am not very good at designing the structure of the design therefore i am asking for advice and opinion Commented Aug 14, 2011 at 16:39
  • @iCezz After you ask a question, you should click the green checkbox next to the answer of your choice to mark it as "accepted". Welcome to Stack Overflow! Commented Aug 14, 2011 at 18:07

3 Answers 3

42

One thing to keep in mind is that a tuple is immutable. This means that once it's created, you can't modify it in-place. A list, on the other hand, is mutable -- meaning you can add elements, remove elements, and change elements in-place. A list has extra overhead, so only use a list if you need to modify the values.

You can create a list of tuples:

>>> list_of_tuples = [(1,2),(3,4)]
>>> list_of_tuples
[(1, 2), (3, 4)]

or a list of lists:

>>> list_of_lists = [[1, 2], [3, 4]]
>>> list_of_lists
[[1, 2], [3, 4]]

The difference is that you can modify the elements in the list of lists:

>>> list_of_lists[0][0] = 7
>>> list_of_lists
[[7, 2], [3, 4]]

but not with the list of tuples:

>>> list_of_tuples[0][0] = 7
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

To iterate over a list of tuples:

>>> for (x,y) in list_of_tuples:
...    print x,y
... 
1 2
3 4
Sign up to request clarification or add additional context in comments.

3 Comments

i just want to store value like text and ID into tuple or list but i want to check it with another for loop. instead of using a for loop in a for loop. i am thinking to use arrays in array. But was pretty confuse whether should i use tuples in list or lists in a list. I am not very good at designing the structure of the design therefore i am asking for advice and opinion
@iCezz I added an example of how you can easily iterate over a list of tuples without multiple for loops.
i think this is a good idea, in this case i can retrieve the value inside a tuple easily and faster without having worry that the data will be changed. thanks again
10

if you are talking about list, you can put anything into it, even different types:

l=[10,(10,11,12),20,"test"]

l[0] = (1,2,3)
l.append((4,5))
l.extend((21,22)) #this one adds each element from the tuple

if you mean array, no python arrays don't support tuples.

1 Comment

thank you for the advice. this is what i mean tuple in an array. basicly it store many tuples in a list. but i thought tuples does not have method therefore i believe that it will add into the list. Thanks for sharing
1
a = [ ('b', i , "ff" ) for i in range(1,5)]  

1 Comment

The OP asked for an array, but this returns a list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.