Python Data Structures
Tags: programming, tutorial, python, beginners
Published: 2022-07-02 06:40:04

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzn9218vypg4ejwtxhre.png)
Output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/or9zt2t4u5g77gq2je4t.png)

Strings also have certain functions which are:
Code:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ydipfvgdn60i6bixrhxh.png)

Output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l9cobxl0rvuy84ci1tk4.png)


**Lists**
I've written a previous article on lists you can read it [here](https://dev.to/ezeanamichael/python-lists-581f)
List comprehension is a quick way of creating a list that obeys a certain rule
An example is
Code:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pqlhoa29rym80viwvu37.png)

Output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6zpvsr3n1lmijptvw1yx.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9z0mkglobtcbwhckh316.png)

Output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4cw272i3q6ofsk7av2sn.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20fw98h09y3lkil0n2a5.png)

Output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1na3ptuhjw31hbz2tdlb.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iuxve8h84p647d36anyp.png)

Output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fnrjck1zqxwhd7elhw3f.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gu1r5fyu1wms9fr0l4yk.png)

Output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tyq5z3hxpaiib81aljt5.png)

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 [here] (https://dev.to/ezeanamichael/a-gpa-caculator-3jia)to 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fchw2ncw31h3vkayf1kh.png)

Output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0bb0mcbs92sqt85sfdz3.png)

Trying to change the value causes an error
Code:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nbb2glr7tzykvjcxh5ip.png)

Output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vm8jynrdi572vx1feav2.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nva0oz6lpg5g5k70b6nd.png)

Output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w25tee94m83zktph3029.png)

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vntuy7na3ibe96vvti7z.png)

Output:
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gzrztawforv8me49cq26.png)

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