Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 9 of 10 · Python Basics & Syntax
Interview question

What is the difference between a Python list, dictionary, and set in terms of use case? Use case के हिसाब से Python list, dictionary, और set में क्या अंतर है?

Answer
StructureOrdered?Duplicates?Access byBest for
listYesAllowedIndexOrdered sequences, need duplicates
dictYes (insertion, Python 3.7+)Keys unique, values can repeatKeyKey-value lookups
setNoNot allowed (auto-dedupes)Membership onlyUniqueness, fast membership tests
# List - ordered, allows duplicates
tags = ['python', 'web', 'python']
print(tags)  # ['python', 'web', 'python']

# Dict - key-value pairs
user = {'name': 'John', 'age': 30}
print(user['name'])  # John

# Set - unique values, fast membership check
unique_tags = set(tags)
print(unique_tags)  # {'python', 'web'}
print('python' in unique_tags)  # O(1) average - much faster than list 'in'

# Choosing based on need
visited_ids = set()  # fast duplicate checking
for user_id in [1, 2, 1, 3]:
    if user_id not in visited_ids:
        visited_ids.add(user_id)
        print(f'Processing {user_id}')
StructureOrdered?Duplicates?Accessसबसे अच्छा
listहाँAllowedIndexOrdered sequences
dictहाँ (insertion order)Keys uniqueKeyKey-value lookups
setनहींAllowed नहींसिर्फ membershipUniqueness, fast checks
tags = ['python', 'web', 'python']
print(tags)  # duplicates रहती हैं

user = {'name': 'John', 'age': 30}
print(user['name'])  # John

unique_tags = set(tags)
print(unique_tags)  # {'python', 'web'}
print('python' in unique_tags)  # तेज़ O(1)

Was this answer clear?