DEV Community

Cover image for List Data Structure in 5 mins - Python
Sakshi
Sakshi

Posted on

9 1 2 1

List Data Structure in 5 mins - Python

Hi developers/learners/readers ๐Ÿ™‹โ€โ™€๏ธโœ‹

Python Unleashed 07

Going to do comparison between C++ vectors and Python lists in this blog. As I am moving to Python from C++, so this can help others like me.

To those who are not familiar with C++, you may find few things confusing.

INTRO

  • List items are within square brackets, like C++ arrays/vector
  • Dynamically sized, can shrink and grow
  • elements are separated by comma as always
  • Stores data in sequence, in linear fashion
  • It can store data of any type, unlike vectors in C++
  • Random Access Possible (Negative and Positive Index), Prone to Index out of bound errors, AND ZERO BASED INDEXING
  • Mutable
  • We do have a list class for lists

List is so so much similar to vectors.

Image description

list[-0] returns 0th elements only

Methods on List

  1. Append -> add element to the end of list, one at a time, tuple and lists can be added

Image description

  1. Insert -> requires two values, element can be added at desired position. One argument is position, second is value.

Image description

  1. Extend -> add multiple elements to the end of list, elements must be in square bracket.

Image description

  1. Reverse -> reverses the list

Image description

  1. Reversed -> A good read

  2. Remove Elements -> Removes one element at a time, and element must be there in the list, otherwise throws error, pass element which you would like to delete

Image description

  1. Pop -> Either remove element from end only or pass index

  2. Slicing -> Same as that for strings check here strings
    For more information check here

END

Thanks for reading

If you liked the content, do react, or for any feedback/correction/suggestion feel free to reach out to me on Linkedin or can comment here

Nice Day

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (4)

Collapse
 
michaeltharrington profile image
Michael Tharrington โ€ข

Heyo โ€” cool post here!

Just a heads up that you can write your code in code blocks with syntax highlighting if you'd like. This will make it easier for folks to copy and run your code.

To do so, you just need to enclose your code with three back ticks before and after + make sure to specify the name of the language to enable syntax highlighting.

For example, writing this:

code block with colors example

... will create this:

console.log(`something`)
Enter fullscreen mode Exit fullscreen mode

Hope this helps! More details in our editor guide!

Collapse
 
bellatrix profile image
Sakshi โ€ข

thanks a lot! will start this for my next blogs

Collapse
 
sh20raj profile image
Sh Raj โ€ข
Collapse
 
sh20raj profile image
Sh Raj โ€ข

Instead of images from carbon or using code highlighting there is one more interactive way to show the code with preview and code runner. So that people can also edit and see the results for satisfaction and clarity.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs