DEV Community

Cover image for Python list methods explanation and visualization
Mahmoud EL-kariouny
Mahmoud EL-kariouny

Posted on

5 1

Python list methods explanation and visualization

Python list methods

Append()

  • Adds an element at the end of the list.
symbols = ['πŸ”΅', '🟨', '🟨']
symbols.append('πŸ”Ί')

# output 
['πŸ”΅', '🟨', '🟨', 'πŸ”Ί']
Enter fullscreen mode Exit fullscreen mode

Extend()

  • Add the elements of a list (or any iterable), to the end of the current list.
symbols = ['πŸ”΅', '🟨', '🟨'] 
add_forms = ['πŸ”Ί', 'πŸ”Ά']
symbols.extend(add_forms)

# output
['πŸ”΅', '🟨', '🟨', 'πŸ”Ί', 'πŸ”Ά']
Enter fullscreen mode Exit fullscreen mode

Insert()

  • Adds an element at the specified position.
symbols = ['πŸ”΅', '🟨', '🟨'] 
symbols.insert(0,  'πŸ”Ί')

# output
['πŸ”Ί', 'πŸ”΅', '🟨', '🟨']
Enter fullscreen mode Exit fullscreen mode

Remove()

  • Removes the first item with the specified value.
symbols =  ['πŸ”΅', '🟨', '🟨', 'πŸ”Ί', 'πŸ”Ά']
symbols.remove('🟨')

# output
['πŸ”΅', '🟨', 'πŸ”Ί', 'πŸ”Ά']
Enter fullscreen mode Exit fullscreen mode

Pop()

  • Removes the element at the specified position.
symbols =  ['πŸ”΅', '🟨', '🟨', 'πŸ”Ί', 'πŸ”Ά']
symbols.pop(0)

# output
['🟨', '🟨', 'πŸ”Ί', 'πŸ”Ά']
Enter fullscreen mode Exit fullscreen mode

Clear()

  • Removes all the elements from the list.
symbols =  ['πŸ”΅', '🟨', '🟨', 'πŸ”Ί', 'πŸ”Ά']
symbols.clear()

# output
[]
Enter fullscreen mode Exit fullscreen mode

Sort()

  • Sorts the list.
symbols =  ['πŸ”΅', '🟨', 'πŸ”Ί', 'πŸ”Ά']
symbols.sort()

# output
['πŸ”΅', 'πŸ”Ά', 'πŸ”Ί', '🟨']
Enter fullscreen mode Exit fullscreen mode

Reverse()

  • Reverses the order of the list.
symbols =  ['πŸ”΅', '🟨', 'πŸ”Ί', 'πŸ”Ά']
symbols.reverse()

# output 
['πŸ”Ά', 'πŸ”Ί', '🟨', 'πŸ”΅']
Enter fullscreen mode Exit fullscreen mode

Index()

  • Returns the index of the first element with the specified value.
symbols =  ['πŸ”΅', '🟨', 'πŸ”Ί', 'πŸ”Ά']
x = symbols.index('πŸ”Ά')

# output
3
Enter fullscreen mode Exit fullscreen mode

Count()

  • Returns the number of elements with the specified value.
symbols =  ['πŸ”΅', '🟨', '🟨', 'πŸ”Ί', 'πŸ”Ά']
x = symbols.count('🟨')

# output
2
Enter fullscreen mode Exit fullscreen mode

copy()

  • Returns a copy of the list.
symbols =  ['πŸ”΅', '🟨', '🟨', 'πŸ”Ί', 'πŸ”Ά', 'πŸ”Ά']
x = symbols.copy()

# output
['πŸ”΅', '🟨', '🟨', 'πŸ”Ί', 'πŸ”Ά', 'πŸ”Ά']
Enter fullscreen mode Exit fullscreen mode

All the best to you.

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post