Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Learn Python #6 - Let's List it...
Nitin Reddy
Nitin Reddy

Posted on

Learn Python #6 - Let's List it...

What are Lists?

Lists in Python are the collection of elements of data. What does that mean? Let's check the below examples.

List

Let us look at an example

  • Example 1:

Let's say we have a list of employees with theirname as the list items

employees = ["Abc", "Bcd"]
Enter fullscreen modeExit fullscreen mode

In the above item, you can add a new item, remove an item, append an item, and furthermore.

Add an item

There are two ways in which we can add a new item to the list.

  • Append
employee.append("Cde")// employee -> ["Abc", "Bcd", "Cde"]
Enter fullscreen modeExit fullscreen mode
  • Insert
employee.insert(0, "Efg")// employee -> ["Efg", "Abc", "Bcd", "Cde"]
Enter fullscreen modeExit fullscreen mode

The difference between theappend andinsert methods is that theappend add the element at the last position of the list whereas ininsert you can specify at what position you want to add the element.

Get the length of the list

employee = ["Efg", "Abc", "Bcd", "Cde"]len(employee) -> 4
Enter fullscreen modeExit fullscreen mode

len is useful that helps in getting the length of a list.

Reference a list item by its position

employee = ["Efg", "Abc", "Bcd", "Cde"]employee[2]
Enter fullscreen modeExit fullscreen mode

In the[] you need to specify a position of an element that you need a reference to.

Changing an item in the list

employee = ["Efg", "Abc", "Bcd", "Cde"]employee[1] = "Nitin"
Enter fullscreen modeExit fullscreen mode

Find the element by specifying the position and then assign the value.

Conclusion

There are more posts on the way related to the lists...So keep reading.

Thanks in advance for reading this article...🚀

I am more than happy to connect with you on

You can also find me on

Top comments(1)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
samuelrivaldo profile image
Samuelrivaldo
Student in web development and Programmation
  • Work
    Student
  • Joined

Thanks 🙏

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Seasoned JavaScript Developer who loves to explore other programming languages like Python. A problem solver, tech lover by heart. Loves reading booking, and cooking. 🤩🚀🔬
  • Location
    Pune, India
  • Education
    CS Graduate
  • Work
    Senior Software Engineer at IglooInsure
  • Joined

More fromNitin Reddy

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp