본문 바로가기
Issue Note

Difference between list and [] (Python)

by jhleeatl 2024. 4. 2.

The difference between `list()` and `[]` lies in their usage and purpose in Python.


list(): 

  - `list()` is a built-in Python function used to convert an iterable object into a list. It takes an iterable object (such as a tuple, string, or range) as an argument and returns a new list containing the elements of that iterable.
  - Example: `list((1, 2, 3))` will return `[1, 2, 3]`.


  
[   ] (square brackets):

  - Square brackets, also known as list literals, are used to directly create new list objects in Python. This is the most straightforward and commonly used method for creating new lists.
  - Example: `[1, 2, 3]` will create a new list containing the elements `1`, `2`, and `3`.

In summary, `list()` is a function used to convert iterable objects into lists, while `[]` is a syntax for directly creating new list objects.

 

 

def solution (a,b):
    result = list(range(a,b))
    return result

print(solution(1,4))

#Result = [1, 2, 3]

 

 

def solution(a, b):
    result = [num for num in range(a, b)]
    return result

print(solution(1, 4))

# result = [1, 2, 3]