What is the difference between a list syntax and a tuple syntax?

Hey I just started learning 3 days ago, and now I’ve made it to the list and tuples section
although this is a legit question, the other purpose of this post is to get acquainted with the python community as a sort of introduction.

Onto the question, in the video I noticed the example for list was

listtA = [5, 10, 15, 20]

however, the example for tuples was

listA =['name ', 'date ', 'city ', 'state ', 'country ’ ]

do to this i am lead to belive the only difference between the two syntaxs is the quotation. am i correct ?

No, that is also a list:

>>> t = ('name', 'date', 'city', 'state', 'country')
>>> type(t)
<class 'tuple'>

Though it should be noted that the round brackets are not explicitly needed, what makes a tuple is simply the commas. People often wrap it with round brackets to separate the data from other things, or simply because our brains like it better that way :upside_down_face:.

>>> t = 'name', 'date', 'city', 'state', 'country'
>>> type(t)
<class 'tuple'>
1 Like

so for clarification

listA = (‘name’ , ‘city’ , ‘state’)

is a tuple because of the ’ i put around each word ?

also thanks for the quick reply !!

From my notes:

List

A list is simply a sequence of values stored in a specific order with each value identified by its position in that order.

A list must be finite. List items are separated by commas and surrounded by square brackets.

Number List
even_numbers = [2,4,6,8,10]

Word List
words = ['the','cat','sat','on','the','mat']

String List
>>> list (‘Hello!’)
['H', 'e', 'l', 'l', 'o', '!']

Lists are …

  • ordered
    • the items have a defined order, and that order will not change.
  • indexed
    • the first item has index [0], the second item has index [1] etc.
  • changeable
    • meaning that we can change, add, and remove items in a list after it has been created.

If you add new items to a list, the new items will be placed at the end of the list.

Since lists are indexed, lists can have items with duplicate values.

List items can be of any data type:

Example

#String, int and boolean data types:
list1 = ["apple", "banana", "cherry"]
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]

A list can contain different data types:

Example

# A list with strings, integers and boolean values:
list1 = ["abc", 34, True, 40, "male", False]

You can change an item in a list by referencing its index number.

numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

n = numbers[4]

print(n)

numbers[4] = 40

n = numbers[4]

print(n)

.append() used to add an item to the end of a list.

To remove or delete a list item, there are two ways to do this:

del list_name[index_number] or list_name.remove(index_number)


Tuple

Tuples are created with round brackets, and the items are ordered, unchangeable and allow duplicate values.

Example

# Create a Tuple:
thistuple = ("apple", "banana", "cherry")
print(thistuple)

Tuples are …

  • ordered
    • the items have a defined order, and that order will not change.
  • indexed
    • the first item has index [0], the second item has index [1] etc.
  • unchangeable
    • meaning that we cannot change, add, or remove items in a Tuple after it has been created.

Since tuples are indexed, tuples can have items with duplicate values of strings, integers and boolean values

Example

# String, int and boolean data types:
tuple2 = ("cherry", "apple", "banana", "cherry")
tuple2 = (1, 5, 7, 9, 3, 1, 0, 9)
tuple3 = (True, False, False)
1 Like

ah so its the brackets then !!

The biggest difference is that a list is changeable and a tuple is not.

It’s easier to spot them in a code block if you use the brackets.

Yes: [ ] for a list and ( ) for a tuple

1 Like

Just to clarify, the round brackets are not responsible for making it a Tuple. Yes, people often use parenthesis when defining tuples, but they are more for organization and separation. You can create tuples without the round brackets, though they will always be visually printed with the round brackets:

>>> t = 'name', 'date', 'city', 'state', 'country'
>>> t
('name', 'date', 'city', 'state', 'country')
>>> type(t)
<class 'tuple'>

But for most, people, [] means lists, and () means Tuples.

But remember, () doesn’t make something a Tuple, even if we associate it with Tuples. They are just organizational symbols:

>>> a = (3)
>>> a
3
>>> type(a)
<class 'int'>

It is the , that makes it a tuple (and the lack of list brackets, set braces, etc.).

2 Likes

hey, i did some searching around and these sites i looked at stated the exact opposite. and echoed @rob42 's notes i can link them if you like

I demonstrated it with actual python in my response. If you don’t believe me, try it yourself.

1 Like

@facelessuser is correct in so much as the brackets alone do not make an object into a tuple, but it’s not usual to define a integer like this: a = (2)

It’s more common to see: a = 2

This will demonstrate.

a = (2)
print(type(a))

a = (1 , 2)
print(type(a))
1 Like

The quotation marks make the items strings.

anumber = 123
astring = '123'

You can have a tuple made of strings, or numbers, or both:

atuple = ('Hello', 'world', 1234, 5678)
1 Like

ok so i did some testing

listA =("apple ", "banana ", "car ")

listL =["apple ", "banana ", "car "]

print(dir(listA))
[‘add’, ‘class’, ‘contains’, ‘delattr’, ‘dir’, ‘doc’, ‘eq’, ‘format’, ‘ge’, ‘getattribute’, ‘getitem’, ‘getnewargs’, ‘gt’, ‘hash’, ‘init’, ‘init_subclass’, ‘iter’, ‘le’, ‘len’, ‘lt’, ‘mul’, ‘ne’, ‘new’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘rmul’, ‘setattr’, ‘sizeof’, ‘str’, ‘subclasshook’, ‘count’, ‘index’]


print(dir(listL))
[‘add’, ‘class’, ‘contains’, ‘delattr’, ‘delitem’, ‘dir’, ‘doc’, ‘eq’, ‘format’, ‘ge’, ‘getattribute’, ‘getitem’, ‘gt’, ‘hash’, ‘iadd’, ‘imul’, ‘init’, ‘init_subclass’, ‘iter’, ‘le’, ‘len’, ‘lt’, ‘mul’, ‘ne’, ‘new’, ‘reduce’, ‘reduce_ex’, ‘repr’, ‘reversed’, ‘rmul’, ‘setattr’, ‘setitem’, ‘sizeof’, ‘str’, ‘subclasshook’, ‘append’, ‘clear’, ‘copy’, ‘count’, ‘extend’, ‘index’, ‘insert’, ‘pop’, ‘remove’, ‘reverse’, ‘sort’]

listL is a true list because it has append. The only thing that i did differently was the brackets.
is there anything I’m missing ? Also, I want to thank you all for your time. I’m glad to know the community is so supportive here

You aren’t missing anything. Yes, switching the brackets will turn the list into a tuple. Removing the brackets will also make it a tuple.

Lists have append as they are mutable and you can add to them. Tuples do not as they are immutable and cannot be modified.

1 Like

The only reason why I make a point about the brackets is that people sometimes get burned and forget that the brackets do not make an item a tuple, while the brackets do make a list a list:

>>> a = [1]
>>> type(a)
<class 'list'>
>>> b = (1)
>>> type(b)
<class 'int'>
>>> c = (1,)
>>> type(c)
<class 'tuple'>
>>> d = 1,
>>> type(d)
<class 'tuple'>
2 Likes

You have no idea how happy I am to be learning this, I appreciate the information you’ve shared.
Now I have an even deeper understanding than what I was going for thanks

Early on, I got mildly singed by the “parentheses make a tuple” misconception. I was just starting out. I can’t recall the exact thing or the extra work it caused me, but I’d gotten it into my head that func1 and func2 had different return types

def func1():
    return 1, 2

def func2():
   return (1, 2)

and that I needed to do things like x, y = func1() versus (x, y) = func2().

My misconception didn’t cause actual problems other than me using way more parenthesis then needed.

Now I know that the use of parentheses around tuples is to communicate to the human reader, and so I use them (or not) as needed for that purpose.

I’ve just finished writing up some notes and as it turns out, demonstrates the use of list and tuple syntax within the functionality.

In this script, you should see that numbList is a list and weights is a tuple

import random

numberList = [100, 200, 300, 400, 500]

outList = (random.choices(numberList, weights=(10, 20, 40, 80, 160), k=10))

print('Results:')
print('100:', outList.count(100))
print('200:', outList.count(200))
print('300:', outList.count(300))
print('400:', outList.count(400))
print('500:', outList.count(500))

As for the script itself:
I’ve chosen a maximum of 10 elements (k=10) with the highest probability (160) set on returning the number 500 from numberList and the lowest probability (10) set on returning the number 100 from numberList.