List with one element python

Python Tuple With One Item

Python Glossary

Create Tuple With One Item

To create a tuple with only one item, you have add a comma after the item, unless Python will not recognize the variable as a tuple.

Example

One item tuple, remember the commma:

thistuple = ["apple",]
print[type[thistuple]]

#NOT a tuple
thistuple = ["apple"]
print[type[thistuple]]
Try it Yourself »

Related Pages

Python Tuples Tutorial Tuple Access Tuple Items Change Tuple Item Loop List Items Check if Tuple Item Exists Tuple Length Remove Tuple Items Join Two Tuples
Python Glossary

Video liên quan

Chủ Đề