What is the difference between sets and lists in Python

0 votes
what is the difference between sets and lists, can't understand.
Feb 8, 2022 in Python by Nandini
• 5,480 points
1,169 views

1 answer to this question.

0 votes

Lists can contain duplicates but sets cannot
Sets are unordered and sets can contain only hashable objects, while lists can contain any kind of object or data type.
Indexing is possible in Lists while not in sets.
Sets allow to perform operations related to set theory like union, intersection, difference etc.
Sets are written in { } (using curly brackets)
Lists are defined as [ ] (using square brackets)

items = {'table','chair','desk'}
items

Output
{'chair', 'desk', 'table'}

type(items)

Output

set
items = ['table','chair','desk']
items

Output

['table', 'chair', 'desk']
type(items)

Output

list


answered Feb 9, 2022 by Dev
• 6,000 points

Related Questions In Python

0 votes
5 answers
+1 vote
1 answer

What is the difference between range and xrange functions in Python 2.X?

xrange only stores the range params and ...READ MORE

answered Aug 22, 2018 in Python by Priyaj
• 58,020 points
2,434 views
+2 votes
2 answers

What is the difference between print and return in python?

Return statements end the execution of a ...READ MORE

answered Aug 26, 2019 in Python by anonymous
8,096 views
0 votes
0 answers

What is the difference between isdigit, isnumeric and isdecimal in python?

Can you give examples for each of ...READ MORE

May 31, 2019 in Python by Waseem
• 4,540 points
2,269 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,464 views
0 votes
1 answer
0 votes
2 answers

What's the difference between %s and %d in Python string formatting?

The reason is that they are using ...READ MORE

answered Feb 8, 2022 in Python by Rahul
• 9,680 points
22,901 views
0 votes
1 answer

What is the python keyword "with" used for?

The PEP343 documentation contains details regarding the ...READ MORE

answered Feb 9, 2022 in Python by Dev
• 6,000 points
735 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP