Home > Software design >  Adding a tuple to list of tuples
Adding a tuple to list of tuples

Time:11-17

I would like add something like this ('first lastname', []) to

[('Bill Gates', [100, 32, 44]), ('Leonardo Dicaprio', [32, 22, 10]), ('Jeff Bezos', [11, 100, 170])]

I have tried this

list = [('Bill Gates', [100, 32, 44]), ('Leonardo Dicaprio', [32, 22, 10]), ('Jeff Bezos', [11, 100, 170])]
variable = input('first lastname') # type "John" and hit enter
tuple(variable)
list.append(tuple(variable))

I got:

[('Bill Gates', [100, 32, 44]), ('Leonardo Dicaprio', [32, 22, 10]), ('Jeff Bezos', [11, 100, 170]), ('J', 'o', 'h', 'n')]

What I expected was:

[('Bill Gates', [100, 32, 44]), ('Leonardo Dicaprio', [32, 22, 10]), ('Jeff Bezos', [11, 100, 170]), ("John", [])]

CodePudding user response:

You can use the append function.

list = [('Bill Gates', [100, 32, 44]), ('Leonardo Dicaprio', [32, 22, 10]), ('Jeff Bezos', [11, 100, 170])]
list.append("first lastname", [])

CodePudding user response:

You are correct to try to append the variable to the list, but your mistake is in trying to turn the string into a tuple.

Apparently Python will take a string and break it up by character if you try to turn it into a tuple.

That is:

>>> tuple('John')
('J', 'o', 'h', 'n')

So, there are two things we can do. If you just want to add 'John' to the end of the list, you can just append it on its own.

(This is from the interactive interpreter, which you get if you type python into a terminal.)

>>> list = [('Bill Gates', [100, 32, 44]), ('Leonardo Dicaprio', [32, 22, 10]), ('Jeff Bezos', [11, 100, 170])]
>>> variable = input('first lastname') # type "John" and hit enter
first lastnameJohn
>>> list.append(variable)
>>> list # show the list
[('Bill Gates', [100, 32, 44]), ('Leonardo Dicaprio', [32, 22, 10]), ('Jeff Bezos', [11, 100, 170]), 'John']

However, if you want to add the tuple (John, []) to the end of the list, you don't have to use the tuple function. Instead, you can just enclose it in parentheses and Python will make it into a tuple like you want without doing anything weird.

That is:

>>> list = [('Bill Gates', [100, 32, 44]), ('Leonardo Dicaprio', [32, 22, 10]), ('Jeff Bezos', [11, 100, 170])]
>>> variable = input('first lastname') # type "John" and hit enter
first lastnameJohn
>>> list.append((variable, []))
>>> list # show the list
[('Bill Gates', [100, 32, 44]), ('Leonardo Dicaprio', [32, 22, 10]), ('Jeff Bezos', [11, 100, 170]), ('John', [])]

In short, I think the code you're looking for is list.append((variable, [])).

However, there's one more thing to note, which is that if you're trying to turn a single object into a tuple, you should put parentheses around it, but you have to add a comma just before the final parenthesis to let Python know that you actually want to make a tuple, rather than just put something in parentheses.

For example, if you want to put 'John' in a tuple, instead of doing tuple('John'), you should instead do ('John',).

Example:

>>> ('John',)
('John',)
  • Related