0% found this document useful (0 votes)
47 views

Collections in Python Are Basically Container Data Types, Namely Lists, Sets, Tuples, Dictionary

Collections in python include lists, sets, tuples, and dictionaries. Lists are ordered and mutable, storing duplicate values that can be accessed by index. Tuples are ordered and unchangeable. Sets are unordered, unchangeable and unindexed. Dictionaries are ordered and changeable. Namedtuples are like objects with named fields that can be accessed by index or lookup. The namedtuple function declares a class with given fields. Counters track how many times equivalent values are added, acting like a dictionary with counts instead of values. OrderedDict remembers insertion order of keys unlike a regular dictionary. Deques support efficient appends and pops from either end unlike regular lists.

Uploaded by

shruti kukkar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Collections in Python Are Basically Container Data Types, Namely Lists, Sets, Tuples, Dictionary

Collections in python include lists, sets, tuples, and dictionaries. Lists are ordered and mutable, storing duplicate values that can be accessed by index. Tuples are ordered and unchangeable. Sets are unordered, unchangeable and unindexed. Dictionaries are ordered and changeable. Namedtuples are like objects with named fields that can be accessed by index or lookup. The namedtuple function declares a class with given fields. Counters track how many times equivalent values are added, acting like a dictionary with counts instead of values. OrderedDict remembers insertion order of keys unlike a regular dictionary. Deques support efficient appends and pops from either end unlike regular lists.

Uploaded by

shruti kukkar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Collections in python are basically container data types, namely lists, sets, tuples, dictionary.

They have different characteristics based on the declaration and the usage. A list is declared in
square brackets, it is mutable, stores duplicate values and elements can be accessed using
indexes.

List is a collection which is ordered and changeable. ...


Tuple is a collection which is ordered and unchangeable. ...
Set is a collection which is unordered, unchangeable*, and unindexed. ...
Dictionary is a collection which is ordered** and changeable.

Nametuple-like object with named fields. These field attributes are accessible by lookup as well
as by index.

General usage of this function is:

Signature:
collections.namedtuple(type_name, field-list)
The following statement declares a student class having name, age and marks as fields.

Example: Declare a Named Tuple


>>> import collections
>>> student = collections.namedtuple('student', [name, age, marks])
To create a new object of this namedtuple, do the following:

Example: Create Object of Named Tuple


>>> s1 = student("Imran", 21, 98)
The values of the field can be accessible by attribute lookup:

Example: Access Named Tuple


>>> s1.name
'Imran'
Or by index:

Example: Access Named Tuple


>>>s1[0]
'Imran'
OrderedDict()
The OrderedDict() function is similar to a normal dictionary object in Python. However, it
remembers the order of the keys in which they were first inserted.

Example: Ordered Dictionary


import collections

d1 = collections.OrderedDict()
d1['A'] = 65
d1['C'] = 67
d1['B'] = 66
d1['D'] = 68

for k,v in d1.items():


print (k,v)
Output
A 65
C 67
B 66
D 68
Upon traversing the dictionary, pairs will appear in the order of their insertion.

deque()
A deque object support appends and pops from either ends of a list. It is more memory efficient
than a normal list object. In a normal list object, the removal of any item causes all items to the
right to be shifted towards left by one index. Hence, it is very slow.

A Counters is a container which keeps track to how many times equivalent values are added.
Python counter class is a part of collections module and is a subclass of dictionary.

Python Counter
We may think of counter as an unordered collection of items where items are stored as
dictionary keys and their count as dictionary value.

The “end” is a keyword that is used in python to place a space after the displayed string instead
of a newline. In python every time you print(“something”), by default it will append a newline.

Here is an example through which you can understand:-

With “end” keyword

print("Hello" , end = ' ')

print("world")

Output:-

Hello world

Without “end” keyword


print(“Hello”)

print(“World”)

Output:-

Hello

world

You might also like