Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unpacking a list
#1
Here's some code:

print()
print('This program will print out a set containing all colors from color_list_1 {"White", "Black", "Red"} that are not present in color_list_2 {"Red", "Green"}.')
color_list_1 = set(["White", "Black", "Red"])
color_list_2 = set(["Red", "Green"])
print()
print('Colors meeting stated criteria are {}.'.format(*[i for i in color_list_1 if i not in color_list_2]))
The result of the last line is "Black"

If I leave the asterisk out, the result of the last line is "['Black', 'White']

What happened to White when I tried to unpack the list?

Thanks!
Reply
#2
You are passing 2 arguments to .format(). Since your format string only has one placeholder, the second argument is ignored.
Reply
#3
Here some variants with f-string.
color_list_1 = set(["White", "Black", "Red"])
color_list_2 = set(["Red", "Green"])
print()
print(f'Colors meeting stated criteria are {[i for i in color_list_1 if i not in color_list_2]}')
print(f'Colors meeting stated criteria are {color_list_1 - color_list_2}')
# Can make sense to make variable outside of string
result = [i for i in color_list_1 if i not in color_list_2]
print(f'Colors meeting stated criteria are: {", ".join(result)}')
Output:
Colors meeting stated criteria are ['White', 'Black'] Colors meeting stated criteria are {'White', 'Black'} Colors meeting stated criteria are: White, Black
Mark17 likes this post
Reply
#4
You can use oeperator'-' or the difference method
Code:
print('\nThis program will print out a set containing all colors from color_list_1 {"White", "Black", "Red"} that are not present in color_list_2 {"Red", "Green"}.')
color_list_1 = set(["White", "Black", "Red"])
color_list_2 = set(["Red", "Green"])
print(color_list_1 - color_list_2)
print(color_list_1.difference(color_list_2))
Output:
Quote:This program will print out a set containing all colors from color_list_1 {"White", "Black", "Red"} that are not present in color_list_2 {"Red", "Green"}.
{'Black', 'White'}
{'Black', 'White'}
Mark17 likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unpacking a dict with * or ** msrk 4 982 Dec-02-2023, 11:50 PM
Last Post: msrk
  iterate through the dict_values while unpacking the dictionary PeacockOpenminded 3 1,293 Jan-22-2023, 12:44 PM
Last Post: PeacockOpenminded
  Unpacking zip object Mark17 12 3,214 Mar-28-2022, 04:59 PM
Last Post: deanhystad
  unpacking list wardancer84 2 1,874 Sep-11-2021, 02:42 PM
Last Post: wardancer84
  unpacking tuple not working project_science 1 1,484 Jan-09-2021, 09:09 PM
Last Post: buran
  Unpacking wheel files DavidTheGrockle 3 11,298 Dec-15-2020, 05:11 PM
Last Post: DavidTheGrockle
  Why the result of "extended iterable unpacking" with set() is unpredictable? zohanlin 2 2,058 Jun-29-2020, 10:30 AM
Last Post: zohanlin
  Unpacking nested lists yonatan776 1 2,197 Apr-14-2020, 08:50 PM
Last Post: buran
  Unpacking dictionary from .xlsx as Kwargs etjkai 5 2,849 Dec-27-2019, 05:31 PM
Last Post: etjkai
  Tuple Unpacking HarshaliPatel 3 2,838 Jan-30-2019, 12:42 PM
Last Post: dukoolsharma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020