DEV Community

Cover image for Python Data Structures
EzeanaMichael
EzeanaMichael

Posted on

Python Data Structures

Data structures are any ways in which data is stored and organized in a format so it can be accessed efficiently.
Some of python's in-built data structures are:

  • String
  • List
  • Dictionaries
  • Tuples
  • Sets

Strings
Strings are written between double quotes or single quotes to store mainly texts.
For example:
Code:
Image description
Output:
Image description

Strings also have certain functions which are:
Code:
Image description

Output:
Image description

Lists
I've written a previous article on lists you can read it here
List comprehension is a quick way of creating a list that obeys a certain rule
An example is
Code:
Image description

Output:
Image description
Where x is the name for each iterated value from 0 to 5 and multiplied by 2 on each iteration.
An "if" statement can be written to specify the contents in the list, It can be written in the form.
Code:
Image description

Output:
Image description

Useful list functions
In the use of strings and lists here are some useful inbuilt functions which can help to write better code:

.split(" ")
Code:
Image description

Output:
Image description

This can be used to join sentences separated by spaces as shown above or"," if the sentence includes it and it is stored on a list which different functions can be performed on them

.join()
This reverses the .split() function which converts text separated into a list, the .join() function converts a list to a group of text or sentences separated by space, and it is written in the format:
Code:
Image description

Output:
Image description

Dictionaries
Dictionaries are data structures used to store data based on a "key:value" pair. They can be indexed and accessed with square brackets just as lists can.
They are contained between curly brackets.
Code:
Image description

Output:
Image description

Dictionaries are best when a logical association of a key:value pair is needed. Dictionaries can take in any data type as the key or value such as a list or integers or strings.
An example is a code I wrote on GPA calculator using string and list pair, click hereto see the post.

Tuples
Tuples are just like lists but instead are written between parentheses "()".They can also be accessed by using square brackets but their values cannot be altered or changed.
Code:
Image description

Output:
Image description

Trying to change the value causes an error
Code:
Image description

Output:
Image description

Variables can be assigned different values in a tuple by assigning in this form. It also supports multiple assignments by using "*variable".
Code:
Image description

Output:
Image description

Sets
Sets just like dictionaries make the use of curly brackets to store their values without the use of key:value pair. It contains a set of unique values, which means the values will always be different, it accepts multiple values but instead outputs it as a set of unique values. Just like the set(a topic in mathematics) it contains useful functions to find:
The union of two sets using ( |) the intersection using(&), the (-) operator for the values in the first set but not in the second, and (^) operator for values in either set but not both.
Code:
Image description

Output:
Image description

These are some of the various data structures in python programming language, read, like, comment, and share.

Top comments (0)